What's new

How traslate this Script to Alibre Script

GIOV

Alibre Super User
Here is a question that I do like solve:
How I do the right translation between this FreeCadPython Script code to a Alibre Python Script Code and what is the principal difference?
It is a simple box part 1x1x1. Unit mm

The code is the following:

'''Examples for a feature class and its view provider.'''

import FreeCAD, FreeCADGui
from pivy import coin

class Box:
def __init__(self, obj):
'''Add some custom properties to our box feature'''
obj.addProperty("App::propertyLength","Length","Box","Length of the box").Length=1.0
obj.addProperty("App::propertyLength","Width","Box","Width of the box").Width=1.0
obj.addProperty("App::propertyLength","Height","Box", "Height of the box").Height=1.0
obj.Proxy = self

def onChanged(self, fp, prop):
'''Do something when a property has changed'''
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
def execute(self, fp):
'''Do something when doing a recomputation, this method is mandatory'''
FreeCAD.Console.PrintMessage("Recompute Python Box feature\n")

class ViewProviderBox:
def __init__(self, obj):
'''Set this object to the proxy object of the actual view provider'''
obj.addProperty("App::propertyColor","Color","Box","Color of the box").Color=(1.0,0.0,0.0)
obj.Proxy = self
def attach(self, obj):
'''Setup the scene sub-graph of the view provider, this method is mandatory'''
self.shaded = coin.SoGroup()
self.wireframe = coin.SoGroup()
self.scale = coin.SoScale()
self.color = coin.SoBaseColor()

data=coin.SoCube()
self.shaded.addChild(self.scale)
self.shaded.addChild(self.color)
self.shaded.addChild(data)
obj.addDisplayMode(self.shaded,"Shaded");
style=coin.SoDrawStyle()
style.style = coin.SoDrawStyle.LINES
self.wireframe.addChild(style)
self.wireframe.addChild(self.scale)
self.wireframe.addChild(self.color)
self.wireframe.addChild(data)
obj.addDisplayMode(self.wireframe,"Wireframe");
self.onChanged(obj,"Color")
def updateData(self, fp, prop):
'''If a property of the handled feature has changed we have the chance to handle this here'''
# fp is the handled feature, prop is the name of the property that has changed
l = fp.getPropertyByName("Length")
w = fp.getPropertyByName("Width")
h = fp.getPropertyByName("Height")
self.scale.scaleFactor.setValue(float(l),float(w),float(h))
pass
def getDisplayModes(self,obj):
'''Return a list of display modes.'''
modes=[]
modes.append("Shaded")
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
'''Return the name of the default display mode. It must be defined in getDisplayModes.'''
return "Shaded"
def setDisplayMode(self,mode):
'''Map the display mode defined in attach with those defined in getDisplayModes.\
Since they have the same names nothing needs to be done. This method is optional'''
return mode
def onChanged(self, vp, prop):
'''Here we can do something when a single property got changed'''
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
if prop == "Color":
c = vp.getPropertyByName("Color")
self.color.rgb.setValue(c[0],c[1],c[2])
def getIcon(self):
'''Return the icon in XPM format which will appear in the tree view. This method is\
optional and if not defined a default icon is shown.'''
return """
/* XPM */
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
" c None",
". c #141010",
"+ c #615BD2",
"@ c #C39D55",
"# c #000000",
"$ c #57C355",
" ........",
" ......++..+..",
" .@@@@.++..++.",
" .@@@@.++..++.",
" .@@ .++++++.",
" ..@@ .++..++.",
"###@@@@ .++..++.",
"##$.@@$#.++++++.",
"#$#$.$$$........",
"#$$####### ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
" #$#$$$$$# ",
" ##$$$$$# ",
" ####### "};
"""
def __getstate__(self):
'''When saving the document this object gets stored using Python's json module.\
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\
to return a tuple of all serializable objects or None.'''
return None
def __setstate__(self,state):
'''When restoring the serialized object from document we have the chance to set some internals here.\
Since no data were serialized nothing needs to be done here.'''
return None


def makeBox():
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box")
Box(a)
ViewProviderBox(a.ViewObject)

makeBox()
 

idslk

Alibre Super User
Hello GIOV,

what is the principal difference?
It hasn't changed...:)
https://www.alibreforum.com/forum/i...freecad-phyton-script-to-alibre-script.20571/
https://www.alibreforum.com/forum/index.php?posts/133059/

Apart from that there a several ways:
  • If you want a skript than write one (There are sample skripts on the Alibrewebsite an here in the forum)
  • describe what you need and maybe someone will do the job for you... (very easy...)
  • or use Freecad if you want Freecadskripts (IMHO not really the goal)

Regards
Stefan
 

idslk

Alibre Super User
had a little coffee break...
Code:
#define function
def make_box():
  #create new part in a new window
  part_box = Part("Box")
  #set units to millimeters
  Units.Current = UnitTypes.Millimeters
  #set dimensions
  length = 10
  width = 10
  heigth = 10
  #add anew sketch on xy-Plane
  box_sketch = part_box.AddSketch('Box_Sketch', part_box.XYPlane)
  #add a rectangle
  box_sketch.AddRectangle(0,0,length,width,False)
  #extrude the rectangle
  box_part = part_box.AddExtrudeBoss('Box', box_sketch, heigth, False)
  #set part color to red
  part_box.SetColor(255,0,0)

#call the defined function 
make_box()

It makes a red cube in a new part window..., but it is a different approach than the one in freecad. It depends on what you wanna get and which system you're using...

Regards
Stefan

PS: If you are posting code, it would be nice to use the insert </>Code function
 

GIOV

Alibre Super User
Thanks Stefan for your post.
It hasn't changed...:)
Ok I understand... My goal is 3D Parts and Assembles Hydrostatics, Stability and FSM on tanks. My be it is a simple Play of Boolean Unite,Subtract, Intersect and Split Features in different water level and angle of heeling. One will be the water level, the other the hull body and the other inside the Tanks with its liquid level. Each heeling angle could be a new configuration. I am thinking about. I will send some ideas internally to you.
Again,
Thanks very much,:)
GIOV
 
Last edited:

GIOV

Alibre Super User
had a little coffee break...
Code:
#define function
def make_box():
#create new part in a new window
part_box = Part("Box")
#set units to millimeters
Units.Current = UnitTypes.Millimeters
#set dimensions
length = 10
width = 10
heigth = 10
#add anew sketch on xy-Plane
box_sketch = part_box.AddSketch('Box_Sketch', part_box.XYPlane)
#add a rectangle
box_sketch.AddRectangle(0,0,length,width,False)
#extrude the rectangle
box_part = part_box.AddExtrudeBoss('Box', box_sketch, heigth, False)
#set part color to red
part_box.SetColor(255,0,0)

#call the defined function
make_box()
Both Codes did the same. In AD11.2 your code run fantastic and it's simple.
I am seeing some potential here.
 
Top