why 1pyramid?(4)array or class

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
mrrclb48z
Posts: 54
Joined: Wed Feb 07, 2018 11:20 am

why 1pyramid?(4)array or class

Post by mrrclb48z »

?1. global name?
?2. I want argument ----> array or class
macro---->short source

I do not understand Python.
Thank you in advance and sorry for the bad english!

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
def myPyramidCal(xIn,yIn,bIn,hIn):
        x = float(xIn)
        y = float(yIn)
        b = float(bIn)
        h = float(hIn)
        x2 = x+b*0.5
        y2 = y+b*0.5
        x3 = x+b
        y3 = y+b
        z = 0
        z2 = h
        P1x=x
        P1y=y
        P1z=z
        P2x=x
        P2y=y3
        P2z=z
        P3x=x3
        P3y=y3
        P3z=z
        P4x=x3
        P4y=y
        P4z=z
        P5x=x2
        P5y=y2
        P5z=z2
        return P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z
def myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z):
        a =[[x,y,z],          #dummy
            [P1x,P1y,P1z],
            [P2x,P2y,P2z],
            [P3x,P3y,P3z],
            [P4x,P4y,P4z],
            [P5x,P5y,P5z]]
        m1 = Mesh.Mesh([a[2],a[5],a[3]])
        m2 = Mesh.Mesh([a[4],a[5],a[1]])
        m3 = Mesh.Mesh([a[1],a[5],a[4]])
        m4 = Mesh.Mesh([a[4],a[3],a[5]])
        m5 = Mesh.Mesh([a[1],a[2],a[3],a[1],a[3],a[4]])
        mesh=Mesh.Mesh()
        mesh.addMesh(m1)
        mesh.addMesh(m2)
        mesh.addMesh(m3)
        mesh.addMesh(m4)
        mesh.addMesh(m5)
        shape = Part.Shape()        
        shape.makeShapeFromMesh(mesh.Topology,0.05)
        solid = Part.makeSolid(shape).removeSplitter()
        myWedgei='x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
        f = App.activeDocument().addObject("Part::Feature",myWedgei)
        f.Shape = solid
def myTxtXYZ(P5x,P5y,P5z):
        myWedgei='x' + str(P5x) + 'y' + str(P5y)+ 'z' + str(P5z)
        p5 = FreeCAD.Vector(P5x,P5y,P5z)
        myText = Draft.makeText(myWedgei,p5) 
        myText.Label = myWedgei

b=1
h=1
P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z=myPyramidCal(0,0,b,h)
myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z)
myTxtXYZ(P5x,P5y,P5z)

App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
Error msg
Traceback (most recent call last): File "xxx.FCMacro",
line 66, in myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z) File "xxx.FCMacro",
line 34, in myPyramidSld a =[[x,y,z], #dummy : global name 'x' is not defined
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: why 1pyramid?(4)array or class

Post by TheMarkster »

The error you are getting is because you are trying to access x, y, and z, but there are no such variables either local to the function, global to the script, or being passed in as parameters to the function. Simplest fix for this is to just pass x, y, and z in as parameters to that function.

Example (bad)

Code: Select all

def myFunction():
    xMin = x #error because there is no such x
Example (good)

Code: Select all

def myFunction(x):
    xMin = x #okay because x is passed in as parameter

Example 3 (good) (creates global x, readable within the function)

Code: Select all

x = 1
def myFunction():
    xMin = x #okay because x is global variable accessible (read only) from within function
If you decide to create a global x (as in example 3) keep in mind if you want to change x inside a function you will need to use the global keyword.

Example 4 (bad if you want to change global x inside function)

Code: Select all

x = 1 #global x
def myFunction():
    x = 2 # does not change global x (no error message, but only creates new local x and sets it to 2)
print x #output is 1
myFunction()
print x #output is still 1 (can be source of many python bugs)
Example 5 (good if you want to change global x within function)

Code: Select all

x = 1 #global x
def myFunction():
    global x #tell python you want to change global x, not create new local x
    x = 2 #changes global x

print x #output is 1
myFunction() #changes global x to 2
print x #output is 2
mrrclb48z
Posts: 54
Joined: Wed Feb 07, 2018 11:20 am

Re: why 1pyramid?(4)array or class

Post by mrrclb48z »

Thank you.
full script :

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
def myPyramidCal(xIn,yIn,bIn,hIn):
        x = float(xIn)
        y = float(yIn)
        b = float(bIn)
        h = float(hIn)
        x2 = x+b*0.5
        y2 = y+b*0.5
        x3 = x+b
        y3 = y+b
        z = 0
        z2 = h
        P1x=x
        P1y=y
        P1z=z
        P2x=x
        P2y=y3
        P2z=z
        P3x=x3
        P3y=y3
        P3z=z
        P4x=x3
        P4y=y
        P4z=z
        P5x=x2
        P5y=y2
        P5z=z2
        return P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z
def myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z):
        xDummy=0
        yDummy=0
        zDummy=0
        a =[[xDummy,yDummy,zDummy],          #dummy
            [P1x,P1y,P1z],
            [P2x,P2y,P2z],
            [P3x,P3y,P3z],
            [P4x,P4y,P4z],
            [P5x,P5y,P5z]]
        m1 = Mesh.Mesh([a[2],a[5],a[3]])
        m2 = Mesh.Mesh([a[4],a[5],a[1]])
        m3 = Mesh.Mesh([a[1],a[5],a[4]])
        m4 = Mesh.Mesh([a[4],a[3],a[5]])
        m5 = Mesh.Mesh([a[1],a[2],a[3],a[1],a[3],a[4]])
        mesh=Mesh.Mesh()
        mesh.addMesh(m1)
        mesh.addMesh(m2)
        mesh.addMesh(m3)
        mesh.addMesh(m4)
        mesh.addMesh(m5)
        shape = Part.Shape()        
        shape.makeShapeFromMesh(mesh.Topology,0.05)
        solid = Part.makeSolid(shape).removeSplitter()
        myWedgei='x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
        f = App.activeDocument().addObject("Part::Feature",myWedgei)
        f.Shape = solid
def myTxtXYZ(P5x,P5y,P5z):
        myWedgei='x' + str(P5x) + 'y' + str(P5y)+ 'z' + str(P5z)
        p5 = FreeCAD.Vector(P5x,P5y,P5z)
        myText = Draft.makeText(myWedgei,p5) 
        myText.Label = myWedgei

b=10
h=10
P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z=myPyramidCal(0,0,b,h)
myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z)
myTxtXYZ(P5x,P5y,P5z)

P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z=myPyramidCal(0,20,b,h)
myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z)
myTxtXYZ(P5x,P5y,P5z)
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: why 1pyramid?(4)array or class

Post by TheMarkster »

One of the faces is missing. I made this change to your code:

Code: Select all

        m3 = Mesh.Mesh([a[1],a[5],a[2]]) #was 1,5,4, but was already a 4,5,1
mrrclb48z
Posts: 54
Joined: Wed Feb 07, 2018 11:20 am

Re: why 1pyramid?(4)array or class

Post by mrrclb48z »

Thank you.
full script:

Code: Select all

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
def myPyramidCal(xIn,yIn,bIn,hIn):
        x = float(xIn)
        y = float(yIn)
        b = float(bIn)
        h = float(hIn)
        x2 = x+b*0.5
        y2 = y+b*0.5
        x3 = x+b
        y3 = y+b
        z = 0
        z2 = h
        P1x=x
        P1y=y
        P1z=z
        P2x=x
        P2y=y3
        P2z=z
        P3x=x3
        P3y=y3
        P3z=z
        P4x=x3
        P4y=y
        P4z=z
        P5x=x2
        P5y=y2
        P5z=z2
        return P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z
def myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z):
        xDummy=0
        yDummy=0
        zDummy=0
        a =[[xDummy,yDummy,zDummy],       #dummy
            [P1x,P1y,P1z],
            [P2x,P2y,P2z],
            [P3x,P3y,P3z],
            [P4x,P4y,P4z],
            [P5x,P5y,P5z]]
        m1 = Mesh.Mesh([a[1],a[2],a[5]])     
        m2 = Mesh.Mesh([a[2],a[3],a[5]])
        m3 = Mesh.Mesh([a[3],a[4],a[5]])
        m4 = Mesh.Mesh([a[4],a[1],a[5]])
        m5 = Mesh.Mesh([a[1],a[2],a[3],a[1],a[3],a[4]])
        mesh=Mesh.Mesh()
        mesh.addMesh(m1)
        mesh.addMesh(m2)
        mesh.addMesh(m3)
        mesh.addMesh(m4)
        mesh.addMesh(m5)
        shape = Part.Shape()        
        shape.makeShapeFromMesh(mesh.Topology,0.05)
        solid = Part.makeSolid(shape).removeSplitter()
        myWedgei='x' + str(P1x) + 'y' + str(P1y)+ 'z' + str(P1z)
        f = App.activeDocument().addObject("Part::Feature",myWedgei)
        f.Shape = solid
def myTxtXYZ(P5x,P5y,P5z):
        myWedgei='x' + str(P5x) + 'y' + str(P5y)+ 'z' + str(P5z)
        p5 = FreeCAD.Vector(P5x,P5y,P5z)
        myText = Draft.makeText(myWedgei,p5) 
        myText.Label = myWedgei

b=10
h=10

P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z=myPyramidCal(0,0,b,h)
myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z)
myTxtXYZ(P5x,P5y,P5z)

P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z=myPyramidCal(0,20,b,h)
myPyramidSld(P1x,P1y,P1z,P2x,P2y,P2z,P3x,P3y,P3z,P4x,P4y,P4z,P5x,P5y,P5z)
myTxtXYZ(P5x,P5y,P5z)

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