Generating Ortographic scetches with Python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Cyberduke
Posts: 11
Joined: Fri May 28, 2021 10:11 am

Generating Ortographic scetches with Python

Post by Cyberduke »

HI, I am not new to python, and I have a step file, for which I need to write a python script that will generate the orthographic drawing from it automatically.

I have kinda figured out the very very basics on how to write a python script to control FreeCAD without a GUI. I also know how to see the generated python code in the Console as I do something manually in the FreeCAD gui. But copying that python code to the GUI-less version gives issues(obviously), and I am not sure how to edit it.

For instance, loading a Step file generated the following code, but running it in python gives an error(I obviously need to remove the GUI elements).
If someone can point me in the right direction I would be really happy.

import Part
from FreeCAD import Base

import ImportGui
ImportGui.open(u"/home/bot.step")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
Gui.SendMsgToActiveView("ViewFit")
Last edited by Cyberduke on Mon May 31, 2021 7:40 am, edited 1 time in total.
Cyberduke
Posts: 11
Joined: Fri May 28, 2021 10:11 am

Re: Generating Ortographic scetches with Python

Post by Cyberduke »

Thank you for the answer, I took some time to work though that example.

Now, I need to get the "TechDraw" workbench going through python. (since that can be used to generate the Ortographic scetch.

I got this sample code, but with the error as shown:

Code: Select all

import FreeCAD
import TechDraw

page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage', 'Page')
FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate', 'Template')
FreeCAD.ActiveDocument.Template.Template = templateFileSpec
FreeCAD.ActiveDocument.Page.Template = FreeCAD.ActiveDocument.Template
page.ViewObject.show()
view = FreeCAD.ActiveDocument.addObject('TechDraw::DrawViewPart', 'View')
rc = page.addView(view)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-43-26d9517177e2> in <module>
2 import TechDraw
3
----> 4 page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage', 'Page')
5 FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate', 'Template')
6 FreeCAD.ActiveDocument.Template.Template = templateFileSpec

AttributeError: 'NoneType' object has no attribute 'addObject'
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Generating Ortographic scetches with Python

Post by onekk »

this will work, but as there is no a templateFileSpec i have commented the line.

Code: Select all

import FreeCAD
import TechDraw

from FreeCAD import Rotation, Vector

FreeCAD.newDocument("test")

DOC = FreeCAD.ActiveDocument

page = DOC.addObject('TechDraw::DrawPage', 'Page')
templ = DOC.addObject('TechDraw::DrawSVGTemplate', 'Template')
#templ.Template = templateFileSpec

page.Template = DOC.Template

page.ViewObject.show()
view = DOC.addObject('TechDraw::DrawViewPart', 'View')

rc = page.addView(view)
As you could see the best way is to name objects using "variables" it is more simple than mimic the console output.

The code above create two document a "base" document. The lack of this document open will raise the error you reported.

the other document is the TechDraw page, for the rest as I don't know TechDraw, I've simply "translated" your code to show the "basics" of a "decent" Python programming.

Hope it helps

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Cyberduke
Posts: 11
Joined: Fri May 28, 2021 10:11 am

Re: Generating Ortographic scetches with Python

Post by Cyberduke »

Thank you, with the addition of commenting out

Code: Select all

#page.ViewObject.show()
, it works. I will have to figure out the TechDraw Workbench in python now:)

I am starting to slowly understand, I modified the code to import the Step file that I have, small question, how can I export a document to for instance PDF or any format so that I can actually "see" what is currently happening and if my import was successful or correct? I anticipate it to be wrong.

Code: Select all

import FreeCAD
import TechDraw
import Part

from FreeCAD import Rotation, Vector

FreeCAD.newDocument("test")
DOC = FreeCAD.ActiveDocument


page = DOC.addObject('TechDraw::DrawPage', 'Page')
templ = DOC.addObject('TechDraw::DrawSVGTemplate', 'Template')

page.Template = DOC.Template

MyShape = Part.read(u"/home/........./bot.step")
MyShape.Volume
MyShape.Area
shapeobj = DOC.addObject("Part::Feature", "MyShape")
shapeobj.Shape = MyShape
DOC.recompute()
#page.ViewObject.show()
view = DOC.addObject('TechDraw::DrawViewPart', 'View')
DOC.recompute()

rc = page.addView(view)
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Generating Ortographic scetches with Python

Post by onekk »

You could inspect some methods using a very easy hack:

in the python console:

Code: Select all

import Part

print(dir(Part))
same thing for a "generic object":

Code: Select all

print(dir(obj_variable))
if there are some docstring or explanation, the same you will see as a popup in the python console, as example:

Code: Select all

print(Part.Circle.__doc__)
willl print in the console:

Code: Select all

Describes a circle in 3D space
To create a circle there are several ways:
Part.Circle()
    Creates a default circle with center (0,0,0) and radius 1

Part.Circle(Circle)
    Creates a copy of the given circle

Part.Circle(Circle, Distance)
    Creates a circle parallel to given circle at a certain distance

Part.Circle(Center,Normal,Radius)
    Creates a circle defined by center, normal direction and radius

Part.Circle(Point1,Point2,Point3)
    Creates a circle defined by three non-linear points
   
hope it helps

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Cyberduke
Posts: 11
Joined: Fri May 28, 2021 10:11 am

Re: Generating Ortographic scetches with Python

Post by Cyberduke »

Thank you, That is definitely helpful in one way, but I think you slightly misunderstood what I tried doing.

So in my code I basically tried to open a file named "bot.step" and I want to put it on the page and then print that page to PDF or something such that I can have an output.
User avatar
onekk
Veteran
Posts: 6197
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Generating Ortographic scetches with Python

Post by onekk »

Sorry I don't know all the method of all the wb of FreeCAD, something is exposed as Python method and you can direct use the methods to do things.

Some other things are achieved in the GUI using "commands" that are script the are run when you hit a button, or select a menu item.

If a "thing" is not exposed as a Python method, maybe you could not use it in scripting or use it in a "limited way" issuing the command, as in:

Code: Select all

Gui.runCommand('Std_Open',0)
But you have to know what command to "issue".

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply