Convert fcstd to techdraw, then to pdf

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
MaximM
Posts: 2
Joined: Thu Sep 19, 2019 1:05 pm

Convert fcstd to techdraw, then to pdf

Post by MaximM »

Hello everyone,
I have been looking for days for a useful answer in this forum for a while, but I just couldn't manage to get an answer.

I have about 700 fcstd files and my task is simple:

Basically, I have to do this with each of these files:
1. Open the file
2. Go to TechDraw
3. Insert New Default Page
4. Select the current object.
5. Insert multiple linked views of drawable objects
6. Add all secondary projections (preferably in a way that does not let any parts hang out of the page)
7. Export/save this page-view to a pdf/jpg/picture-file.


I wanted a macro, that allows me to open 50 files, perform the above actions for each of these files.
I am a total noob concerning Python, but I am learning. I thought, I could manage to do this on my own, but I need a lot more help than expected.
Can somebody please help me?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Convert fcstd to techdraw, then to pdf

Post by vocx »

MaximM wrote: Thu Sep 19, 2019 1:16 pm ...
I am a total noob concerning Python, but I am learning. I thought, I could manage to do this on my own, but I need a lot more help than expected.
Can somebody please help me?
You should show at least what you have tried.

Whenever you perform an action in FreeCAD, the operations are shown in the Python console. So, normally you would take these actions and copy them to a file. That's the whole point of writing macros.

For example, if I just perform the steps manually of opening the file, opening a TechDraw page, creating a projection, setting the scale, etc. I get the following output. You could do the same, and see a similar output.

The lines that start with # are comments so they don't actually do anything. They just show the actions that are occurring.

If this process is good, then you can call the same macro every time, just changing the name of the .FCstd file with your object.

Code: Select all

Python 3.6.8 (default, Aug 20 2019, 17:12:48) 
[GCC 8.3.0] on linux
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>> Gui.runCommand('Std_ViewStatusBar',1)
>>> Gui.runCommand('Std_ViewStatusBar',0)
>>> Gui.runCommand('Std_Workbench',19)
>>> ### Begin command Std_New
>>> # Gui/CommandDoc.cpp(427)
>>> App.newDocument("Unnamed")
>>> App.setActiveDocument("Unnamed")
>>> App.ActiveDocument=App.getDocument("Unnamed")
>>> Gui.ActiveDocument=Gui.getDocument("Unnamed")
>>> # Gui/CommandDoc.cpp(428)
>>> Gui.activeDocument().activeView().viewDefaultOrientation()
>>> ### End command Std_New
>>> Gui.runCommand('Std_OrthographicCamera',1)
>>> ### Begin command Std_Open
>>> # Gui/Application.cpp(513)
>>> App.closeDocument('Unnamed')
>>> App.setActiveDocument("")
>>> App.ActiveDocument=None
>>> Gui.ActiveDocument=None
>>> # Gui/Application.cpp(520)
>>> import FreeCAD
>>> # Gui/Application.cpp(523)
>>> FreeCAD.open(u"/home/vocx/auto_fusion.FCStd")
>>> App.setActiveDocument("auto_fusion")
>>> App.ActiveDocument=App.getDocument("auto_fusion")
>>> Gui.ActiveDocument=Gui.getDocument("auto_fusion")
>>> ### End command Std_Open
>>> ### Begin command Std_Workbench
>>> # Gui/CommandStd.cpp(99)
>>> Gui.activateWorkbench("TechDrawWorkbench")
>>> ### End command Std_Workbench
>>> ### Begin command TechDraw_NewPageDef
>>> # Mod/TechDraw/Gui/Command.cpp(123)
>>> App.activeDocument().addObject('TechDraw::DrawPage','Page')
>>> # Mod/TechDraw/Gui/Command.cpp(124)
>>> App.activeDocument().addObject('TechDraw::DrawSVGTemplate','Template')
>>> # Mod/TechDraw/Gui/Command.cpp(126)
>>> App.activeDocument().Template.Template = '/opt/freecad-build-vocx/share/Mod/TechDraw/Templates/A4_LandscapeTD.svg'
>>> # Mod/TechDraw/Gui/Command.cpp(127)
>>> App.activeDocument().Page.Template = App.activeDocument().Template
>>> ### End command TechDraw_NewPageDef
>>> Gui.Selection.addSelection('auto_fusion','Body')
>>> ### Begin command TechDraw_ProjGroup
>>> # Mod/TechDraw/Gui/Command.cpp(563)
>>> App.activeDocument().addObject('TechDraw::DrawProjGroup','ProjGroup')
>>> # Mod/TechDraw/Gui/Command.cpp(564)
>>> App.activeDocument().Page.addView(App.activeDocument().ProjGroup)
>>> # Mod/TechDraw/Gui/Command.cpp(569)
>>> App.activeDocument().ProjGroup.addProjection('Front')
>>> # Mod/TechDraw/Gui/Command.cpp(584)
>>> App.activeDocument().ProjGroup.Anchor.Direction = FreeCAD.Vector(-0.440,-0.375,0.816)
>>> # Mod/TechDraw/Gui/Command.cpp(586)
>>> App.activeDocument().ProjGroup.Anchor.RotationVector = FreeCAD.Vector(0.649,-0.761,-0.000)
>>> # Mod/TechDraw/Gui/Command.cpp(588)
>>> App.activeDocument().ProjGroup.Anchor.recompute()
>>> ### End command TechDraw_ProjGroup
>>> # Mod/TechDraw/Gui/TaskProjGroup.cpp(474)
>>> Gui.ActiveDocument.resetEdit()
>>> # Gui/CommandDoc.cpp(1299)
>>> App.activeDocument().recompute(None,True,True)
>>> Gui.Selection.clearSelection()
>>> Gui.Selection.addSelection('auto_fusion','Page')
>>> # Gui/propertyeditor/PropertyItem.cpp(484)
>>> FreeCAD.getDocument("auto_fusion").getObject("Page").Scale = 0.5000
>>> Gui.Selection.clearSelection()
>>> Gui.Selection.addSelection('auto_fusion','ProjItem')
>>> 
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
wandererfan
Veteran
Posts: 6324
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Convert fcstd to techdraw, then to pdf

Post by wandererfan »

MaximM wrote: Thu Sep 19, 2019 1:16 pm I wanted a macro, that allows me to open 50 files, perform the above actions for each of these files.
I am a total noob concerning Python, but I am learning. I thought, I could manage to do this on my own, but I need a lot more help than expected.
https://github.com/FreeCAD/FreeCAD/blob ... oupTest.py should get you started. It does most of the things on your list.

I'll have to think a bit about how to tell if anything is hanging off the edge of the page.

A macro that makes 50 pages of drawings is going to run for a long time. You might want to consider doing it in smaller groups.
MaximM
Posts: 2
Joined: Thu Sep 19, 2019 1:05 pm

Re: Convert fcstd to techdraw, then to pdf

Post by MaximM »

EDIT:
Never mind, I did!

In order to close this thread, I will post my resulting code:

Code: Select all

i = 0
templateFile = App.getResourceDir() + "Mod/TechDraw/Templates/A3_Landscape_ISO7200TD.svg"
for obj_name in App.listDocuments():
	i += 1
	App.setActiveDocument(obj_name)
	obj = FreeCAD.ActiveDocument.Objects[0]
	page = FreeCAD.ActiveDocument.addObject('TechDraw::DrawPage','page')
	page.Label=obj.Label
	template = FreeCAD.ActiveDocument.addObject('TechDraw::DrawSVGTemplate','Template')
	template.Template = templateFile
	page.Template = template
	page.ViewObject.show()
	featName = obj.Name
	doc = App.ActiveDocument
	doc.openTransaction("Create Proj Group")
	groupName = 'ProjGroup'
	group = FreeCAD.ActiveDocument.addObject('TechDraw::DrawProjGroup', groupName)
	rc = page.addView(group)
	group.Source = [obj]
	frontView = group.addProjection("Front")
	anchorDir = FreeCAD.Vector(0.0, 0.0, 1.0);
	anchorRot = FreeCAD.Vector(1.0, 0.0, 0.0);
	group.Anchor.Direction = anchorDir
	group.Anchor.RotationVector = anchorRot
	group.Anchor.recompute()
	doc.commitTransaction()
	#add additional views
	leftView = group.addProjection("Left")
	topView = group.addProjection("Top")
	rightView = group.addProjection("Right")
	rearView = group.addProjection("Rear")
	BottomView = group.addProjection("Bottom")
	FrontBottomLeftView = group.addProjection("FrontBottomLeft")
	FrontBottomRightView = group.addProjection("FrontBottomRight")
	FrontTopRightView = group.addProjection("FrontTopRight")
	FrontTopLeftView = group.addProjection("FrontTopLeft")
	#save the PDF
	__objs__=[]
	__objs__.append(page)
	import FreeCADGui
	FreeCADGui.export(__objs__,u"C:/Users/user_name/Desktop/target_folder/" + page.Label + "_" + str(i) + ".pdf")
	del __objs__
EDIT 2:
I almost forgot to thank you for you help/hints.
Please, be gentle to others when they have questions.
FreeCAD does not capture inputs from the TechDraw view in the python console, and the documentation within the Wiki uses obsolete python syntax.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Convert fcstd to techdraw, then to pdf

Post by Kunda1 »

#documentation
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
PhilCera
Posts: 12
Joined: Wed Jun 12, 2019 10:41 am
Location: Belgique

Re: Convert fcstd to techdraw, then to pdf

Post by PhilCera »

Thanks!
It will help me to build batch works macro with TechDraw....
Philippe
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Convert fcstd to techdraw, then to pdf

Post by Kunda1 »

Would someone from the TD be willing to update the wiki with the above info ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply