DrawingExample.py

Go to the documentation of this file.
00001 # exampel how to use the scripting API of the drawing module
00002 # 
00003 # first of all you need the Part and the Drawing module:
00004 import FreeCAD, Part, Drawing
00005 
00006 # create a small sample part
00007 Part.show(Part.makeBox(100,100,100).cut(Part.makeCylinder(80,100)).cut(Part.makeBox(90,40,100)).cut(Part.makeBox(20,85,100)))
00008 
00009 # direct projection. The G0 means hard edge, the G1 is tangend continues.
00010 Shape = App.ActiveDocument.Shape.Shape
00011 [visiblyG0,visiblyG1,hiddenG0,hiddenG1] = Drawing.project(Shape)
00012 print "visible edges:", len(visiblyG0.Edges)
00013 print "hidden edges:", len(hiddenG0.Edges)
00014 # all was projected on the Z-plane:
00015 print "Bnd Box shape: X=",Shape.BoundBox.XLength," Y=",Shape.BoundBox.YLength," Z=",Shape.BoundBox.ZLength
00016 print "Bnd Box project: X=",visiblyG0.BoundBox.XLength," Y=",visiblyG0.BoundBox.YLength," Z=",visiblyG0.BoundBox.ZLength
00017 
00018 # different projection vector
00019 [visiblyG0,visiblyG1,hiddenG0,hiddenG1] = Drawing.project(Shape,Base.Vector(1,1,1))
00020 
00021 # project to SVG
00022 resultSVG = Drawing.projectToSVG(Shape,App.Vector(1,1,1))
00023 print resultSVG
00024 
00025 # And now the parametric way
00026 # 
00027 # insert a Page object and assign a template
00028 App.activeDocument().addObject('Drawing::FeaturePage','Page')
00029 App.activeDocument().Page.Template = App.ConfigGet('AppHomePath')+'Mod/Drawing/Templates/A3_Landscape.svg'
00030 
00031 # create a view on the "Shape" object, define the position and scale and assign it to a Page
00032 App.activeDocument().addObject('Drawing::FeatureViewPart','View')
00033 App.activeDocument().View.Source = App.activeDocument().Shape
00034 App.activeDocument().View.Direction = (0.0,0.0,1.0)
00035 App.activeDocument().View.X = 10.0
00036 App.activeDocument().View.Y = 10.0
00037 App.activeDocument().Page.addObject(App.activeDocument().View)
00038 
00039 # create a second view on the same object but the view is 
00040 # rotatet 90 degrees.
00041 App.activeDocument().addObject('Drawing::FeatureViewPart','ViewRot')
00042 App.activeDocument().ViewRot.Source = App.activeDocument().Shape
00043 App.activeDocument().ViewRot.Direction = (0.0,0.0,1.0)
00044 App.activeDocument().ViewRot.X = 290.0
00045 App.activeDocument().ViewRot.Y = 30.0
00046 App.activeDocument().ViewRot.Scale = 1.0
00047 App.activeDocument().ViewRot.Rotation = 90.0
00048 App.activeDocument().Page.addObject(App.activeDocument().ViewRot) 
00049 
00050 # create a third view on the same object but with an isometric
00051 # view direction. Also the hidden lines are activated.
00052 
00053 App.activeDocument().addObject('Drawing::FeatureViewPart','ViewIso')
00054 App.activeDocument().ViewIso.Source = App.activeDocument().Shape
00055 App.activeDocument().ViewIso.Direction = (1.0,1.0,1.0)
00056 App.activeDocument().ViewIso.X = 335.0
00057 App.activeDocument().ViewIso.Y = 140.0
00058 App.activeDocument().ViewIso.ShowHiddenLines = True
00059 App.activeDocument().Page.addObject(App.activeDocument().ViewIso) 
00060 
00061 # change something and update.
00062 # The update process change the view and the page
00063 App.activeDocument().View.X = 30.0
00064 App.activeDocument().View.Y = 30.0
00065 App.activeDocument().View.Scale = 1.5
00066 App.activeDocument().recompute()
00067 
00068 # Accessing the bits and peaces:
00069 # get the SVG fragment of a single view
00070 ViewSVG = App.activeDocument().View.ViewResult
00071 print ViewSVG
00072 
00073 # get the hole result page (its a file in the document temp dir, only read allowed)
00074 print "Resulting SVG document: ",App.activeDocument().Page.PageResult
00075 file = open(App.activeDocument().Page.PageResult,"r")
00076 print "Result page is ",len(file.readlines())," lines long"
00077 # important, give free the file!
00078 del file
00079 
00080 # insert a view with your own content:
00081 App.activeDocument().addObject('Drawing::FeatureView','ViewSelf')
00082 App.activeDocument().ViewSelf.ViewResult = """<g id="ViewSelf"
00083   stroke="rgb(0, 0, 0)"
00084   stroke-width="0.35"
00085   stroke-linecap="butt"
00086   stroke-linejoin="miter"
00087   transform="translate(30,30)"
00088   fill="#00cc00"
00089  >
00090 
00091 <ellipse cx="40" cy="40" rx="30" ry="15"/>
00092 </g>
00093 """
00094 App.activeDocument().Page.addObject(App.activeDocument().ViewSelf)
00095 
00096 App.activeDocument().recompute()
00097 
00098 del Shape,ViewSVG, resultSVG
00099 

Generated on Wed Nov 23 19:00:09 2011 for FreeCAD by  doxygen 1.6.1