Unnamedの使い方を教えて下さい。

Japanese forum
Post Reply
mrrclb48z
Posts: 39
Joined: Wed Feb 07, 2018 11:20 am

Unnamedの使い方を教えて下さい。

Post by mrrclb48z »

Unnamedを変更するとうごきません。
参考になるヘルプがあれば、教えてください。

Code: Select all


import FreeCAD
import Part
import DraftTools
import Draft
import Mesh

def myTetrahedron(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z):
    xDummy=0
    yDummy=0
    zDummy=0
    a =[[xDummy,yDummy,zDummy],  #dummy
        [P1x,P1y,P1z],
        [P2x,P2y,P2z],
        [P3x,P3y,P3z],
        [P4x,P4y,P4z]]
    m1 = Mesh.Mesh([a[1],a[2],a[3]])     
    m2 = Mesh.Mesh([a[1],a[2],a[4]])
    m3 = Mesh.Mesh([a[2],a[3],a[4]])
    m4 = Mesh.Mesh([a[3],a[1],a[4]])
    mesh=Mesh.Mesh()
    mesh.addMesh(m1)
    mesh.addMesh(m2)
    mesh.addMesh(m3)
    mesh.addMesh(m4)
    shape = Part.Shape()        
    shape.makeShapeFromMesh(mesh.Topology,0.05)
    solid = Part.makeSolid(shape).removeSplitter()
    myWedgei='myTetrahedron_'+'x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
    f = App.activeDocument().addObject("Part::Feature",myWedgei)
    f.Shape = solid
    FreeCADGui.getDocument('Unnamed').getObject('myTetrahedron_x0_0y0_0z5_0').ShapeColor = (1.00,1.00,0.50)
    target_object=App.ActiveDocument.getObjectsByLabel("myTetrahedron_x0_0y0_0z5_0")[0]
    myVolume=target_object.Shape.Volume
    myTxt_XYZ(0,0,0,"{:.3f}".format(myVolume)+'cm3')

def myCube(mySize):
    App.ActiveDocument.addObject("Part::Box","Box")
    App.getDocument('Unnamed').Box.Length=mySize
    App.getDocument('Unnamed').Box.Width=mySize
    App.getDocument('Unnamed').Box.Height=mySize
    App.getDocument('Unnamed').Box.Placement=App.Placement(App.Vector(0.00,0.00,0.00),App.Rotation(App.Vector(0.00,0.00,1.00),0.00))
    FreeCADGui.getDocument('Unnamed').getObject('Box').Transparency = 100

def myTxt_XYZ(P5x,P5y,P5z,myWedgei):
    p5 = FreeCAD.Vector(P5x,P5y,P5z)
    myText = Draft.makeText(myWedgei,p5) 
    myText.Label = myWedgei
    FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.7 mm'

# [(0,0,5),(6,0,2),(6,6,4),(0,6,0)]
P1x,P1y,P1z=0.0,0.0,5.0
P2x,P2y,P2z=6.0,0.0,2.0
P3x,P3y,P3z=6.0,6.0,4.0
P4x,P4y,P4z=0.0,6.0,0.0

myTetrahedron(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z)
myTxt_XYZ(P1x,P1y,P1z,'x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z))
myTxt_XYZ(P2x,P2y,P2z,'x' + str(P2x) + 'y' + str(P2y)+ 'z' + str(P2z))
myTxt_XYZ(P3x,P3y,P3z,'x' + str(P3x) + 'y' + str(P3y)+ 'z' + str(P3z))
myTxt_XYZ(P4x,P4y,P4z,'x' + str(P4x) + 'y' + str(P4y)+ 'z' + str(P4z))
myCube(6.0)

App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")

User avatar
h.tsubota
Posts: 68
Joined: Mon Mar 21, 2016 2:09 pm

Re: Unnamedの使い方を教えて下さい。

Post by h.tsubota »

Unnamed はドキュメントの名前なので、Unnamed が無くても動くようにしたければ例えば activeDocument() でドキュメントを取るよう変更にすればよいのではないでしょうか。

Windows 版の FreeCAD 0.19.1 では以下のようにすれば任意のアクティブなドキュメントでエラー無く動作しました。

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh

def myTetrahedron(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z):
    doc = App.activeDocument()
    gui_doc=Gui.activeDocument()

    xDummy=0
    yDummy=0
    zDummy=0
    a =[[xDummy,yDummy,zDummy],  #dummy
        [P1x,P1y,P1z],
        [P2x,P2y,P2z],
        [P3x,P3y,P3z],
        [P4x,P4y,P4z]]
    m1 = Mesh.Mesh([a[1],a[2],a[3]])     
    m2 = Mesh.Mesh([a[1],a[2],a[4]])
    m3 = Mesh.Mesh([a[2],a[3],a[4]])
    m4 = Mesh.Mesh([a[3],a[1],a[4]])
    mesh=Mesh.Mesh()
    mesh.addMesh(m1)
    mesh.addMesh(m2)
    mesh.addMesh(m3)
    mesh.addMesh(m4)
    shape = Part.Shape()        
    shape.makeShapeFromMesh(mesh.Topology,0.05)
    solid = Part.makeSolid(shape).removeSplitter()
    myWedgei='myTetrahedron_'+'x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
    f = doc.addObject("Part::Feature",myWedgei)
    f.Shape = solid
    gui_doc.getObject('myTetrahedron_x0_0y0_0z5_0').ShapeColor = (1.00,1.00,0.50)
    target_object=doc.getObjectsByLabel("myTetrahedron_x0_0y0_0z5_0")[0]
    myVolume=target_object.Shape.Volume
    myTxt_XYZ(0,0,0,"{:.3f}".format(myVolume)+'cm3')

def myCube(mySize):
    doc = App.activeDocument()
    gui_doc=Gui.activeDocument()
    
    doc.addObject("Part::Box","Box")
    doc.Box.Length=mySize
    doc.Box.Width=mySize
    doc.Box.Height=mySize
    doc.Box.Placement=App.Placement(App.Vector(0.00,0.00,0.00),App.Rotation(App.Vector(0.00,0.00,1.00),0.00))
    gui_doc.getObject('Box').Transparency = 100

def myTxt_XYZ(P5x,P5y,P5z,myWedgei):
    p5 = FreeCAD.Vector(P5x,P5y,P5z)
    myText = Draft.makeText(myWedgei,p5) 
    myText.Label = myWedgei
    FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.7 mm'

# [(0,0,5),(6,0,2),(6,6,4),(0,6,0)]
P1x,P1y,P1z=0.0,0.0,5.0
P2x,P2y,P2z=6.0,0.0,2.0
P3x,P3y,P3z=6.0,6.0,4.0
P4x,P4y,P4z=0.0,6.0,0.0

myTetrahedron(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z)
myTxt_XYZ(P1x,P1y,P1z,'x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z))
myTxt_XYZ(P2x,P2y,P2z,'x' + str(P2x) + 'y' + str(P2y)+ 'z' + str(P2z))
myTxt_XYZ(P3x,P3y,P3z,'x' + str(P3x) + 'y' + str(P3y)+ 'z' + str(P3z))
myTxt_XYZ(P4x,P4y,P4z,'x' + str(P4x) + 'y' + str(P4y)+ 'z' + str(P4z))
myCube(6.0)

App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
mrrclb48z
Posts: 39
Joined: Wed Feb 07, 2018 11:20 am

Re: Unnamedの使い方を教えて下さい。

Post by mrrclb48z »

ありがとうございました。
①(オリジナルポスト)「【今年の1問】 2021年開成中-三角すいの体積」を参考にWolframAlphaとonline sympyとFreeCAD でやってみた。
https://qiita.com/mrrclb48z/items/159ea13c0fb80b3a8f0d

②winmergeしました。
(タイトル)Qiita上にhtm文の投稿方法を教えて下さい。
https://qiita.com/mrrclb48z/questions/3 ... 672364865a
Post Reply