Difference between revisions of "Code snippets"

From FreeCAD Documentation
Jump to navigation Jump to search
Line 34: Line 34:
  return {'Pixmap' : 'path_to_an_icon/myicon.png', 'MenuText': 'Short text', 'ToolTip': 'More detailed text'}  
  return {'Pixmap' : 'path_to_an_icon/myicon.png', 'MenuText': 'Short text', 'ToolTip': 'More detailed text'}  
FreeCADGui.AddCommand('Script_Cmd', ScriptCmd())
FreeCADGui.AddCommand('Script_Cmd', ScriptCmd())
=== Import a new filetype ===
=== Import a new filetype ===

Revision as of 11:29, 26 September 2007

This page contains examples, pieces, chunks of FreeCAD python code collected from users experiences and discussions on the forums. Read and use it as a start for your own scripts...

A typical InitGui.py file

Every module must contain, besides your main module file, an InitGui.py file, responsible for inserting the module in the main Gui. This is an example of a simple one.

class ScriptWorkbench ( Workbench ): 
	def Activate(self): 
			import Scripts # assuming Scripts.py is your module
			if Gui.HasWorkbench('Scripts') == False:
				w = Gui.CreateWorkbench('Scripts') 
				list = ["Script_Cmd"] # That list must contain command names, that can be defined in Scripts.py
				w.AppendToolbar("My Scripts",list) 
	def GetClassName(self): 
		return "Gui::PythonWorkbench" 

A typical module file

This is an example of a main module file, containing everything your module does. It is the Scripts.py file invoked by the previous example. You can have all your custom commands here.

import FreeCAD, FreeCADGui 

class ScriptCmd: 
	def Activated(self): 
		# Here your write what your ScriptCmd does...
	def GetResources(self): 
		return {'Pixmap' : 'path_to_an_icon/myicon.png', 'MenuText': 'Short text', 'ToolTip': 'More detailed text'} 
FreeCADGui.AddCommand('Script_Cmd', ScriptCmd())

Import a new filetype

Making an importer for a new filetype in FreeCAD is easy. FreeCAD doesn't consider that you import data in an opened document, but rather that you simply can directly open the new filetype. So what you need to do is to add the new file extension to FreeCAD's list of known extensions, and write the code that will read the file and create the FreeCAD objects you want:

This line must be added to the InitGui.py file to add the new file extension to the list:

# Assumes Import_Ext.py is the file that has the code for opening and reading .ext files
FreeCAD.EndingAdd("Your new File Type (*.ext)","Import_Ext") 

Then in the Import_Ext.py file:

def open(filename): 
	# here you do all what is needed with filename, read, classify data, create corresponding FreeCAD objects

Adding a line

A line simply has 2 points.

import Part,PartGui 
# add a line element to the document and set its points 

Adding a polygon

A polygon is simply a set of connected line segments (a polyline in AutoCAD). It doesn't need to be closed.

import Part,PartGui 
# create a 3D vector, set its coordinates and add it to the list 
#... repeat for all nodes 
# Create a polygon object and set its nodes 

Adding an object to a group

grp=doc.addObject("App::DocumentObjectGroup", "Group") 
l=grp.addObject("Part::Line", "Line") 

Adding a Mesh

import Mesh
# create a new empty mesh
m = Mesh.newMesh()
# build up box out of 12 facets
m.addFacet(0.0,0.0,0.0, 0.0,0.0,1.0, 0.0,1.0,1.0)
m.addFacet(0.0,0.0,0.0, 0.0,1.0,1.0, 0.0,1.0,0.0)
m.addFacet(0.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,1.0)
m.addFacet(0.0,0.0,0.0, 1.0,0.0,1.0, 0.0,0.0,1.0)
m.addFacet(0.0,0.0,0.0, 0.0,1.0,0.0, 1.0,1.0,0.0)
m.addFacet(0.0,0.0,0.0, 1.0,1.0,0.0, 1.0,0.0,0.0)
m.addFacet(0.0,1.0,0.0, 0.0,1.0,1.0, 1.0,1.0,1.0)
m.addFacet(0.0,1.0,0.0, 1.0,1.0,1.0, 1.0,1.0,0.0)
m.addFacet(0.0,1.0,1.0, 0.0,0.0,1.0, 1.0,0.0,1.0)
m.addFacet(0.0,1.0,1.0, 1.0,0.0,1.0, 1.0,1.0,1.0)
m.addFacet(1.0,1.0,0.0, 1.0,1.0,1.0, 1.0,0.0,1.0)
m.addFacet(1.0,1.0,0.0, 1.0,0.0,1.0, 1.0,0.0,0.0)
# scale to a edge langth of 100
# add the mesh to the active document

Adding an arc or a circle

import Part
doc = App.activeDocument()
c = Part.circle() # create a circle object with undefined radius 
f = doc.addObject("Part::Circle", "Circle") # create a document with a circle feature 
f.Circ = c # Assign the circle object to the Circ property 

Accessing and changing representation of an object

Each object in a FreeCAD document has an associated view representation object that stores all the parameters that define how the object appear, like color, linewidth, etc...

doc=Gui.activeDocument()   # access the active document containing all 
                           # view representations of the features in the
                           # corresponding App document 
f=doc.getObject("Cube")    # access the view representation to the Mesh feature 'Cube' 
f.ShapeColor               # prints the color to the console 
f.ShapeColor=(1.0,1.0,1.0) # sets the shape color to white