Export STEP file without Gui and preserve structure.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Export STEP file without Gui and preserve structure.

Post by keithsloan52 »

i am trying to develop a standalone utility to convert a GDML file to Step,

But so far in all attempts I am unable to preserve structure. The test structure I am dealing with looks like
Volumes.png
Volumes.png (31.69 KiB) Viewed 1306 times
If I use the GDML workbench i.e. with Gui it works. In this case the ImportGui.export function is used but this is only available when the
Gui is active so cannot be used. The exported Step file looks like
FCexport.step
(36.34 KiB) Downloaded 29 times
This thread https://forum.freecadweb.org/viewtopic.php?t=32710 Has some examples of exporting STEP without the Gui,
But using this approach I am unclear how to retain structure. The post says there are three ways of doing this

Code: Select all

Part.export([testbox],"C:/Users/vad/Desktop/testbox/box.step")

testbox.Shape.exportStep("C:/Users/vad/Desktop/testbox/box.step")

import Import
Import.export([testbox],"C:/Users/vad/Desktop/testbox/box.step")
But what should the list look like if structure is to be preserved?

If I code as follows the structure does not get preserved.

Code: Select all

def stackShapes(objList) :
    # Stack shapes to global shapesList
    for x in objList :
        if x.TypeId == 'App::Part' :
           print("Part : "+x.Name)
           shapesList.append(x)

        if hasattr(x,'Shape') :
           print('keep')
           print(x.TypeId)
           print(x.Name)
           shapesList.append(x)

        if len(x.OutList) > 0 :
           stackShapes(x.OutList)

# iterate through all objects
for o in App.ActiveDocument.Objects:
  # find root object and export the shape
  #print(o.Name)
  if o.Name == 'Volumes' :
     global shapesList
     shapesList = []
     print(o.TypeId)
     if len(o.OutList) > 0 :
        stackShapes(o.OutList)
     if len(shapesList) > 0 :
        Part.export(shapesList,"/tmp/test4.step")
Out of interest where would I find the source to ImportGui.export?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export STEP file without Gui and preserve structure.

Post by keithsloan52 »

@easyw-fc With your experience of exporting STEP for KiCAD are you able to advise?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Export STEP file without Gui and preserve structure.

Post by keithsloan52 »

Closed where as Part.export will only deal with flat structure Import.export will.
danidr
Posts: 40
Joined: Thu Aug 25, 2016 10:46 am

Re: Export STEP file without Gui and preserve structure.

Post by danidr »

Hello everyone,

I am resurrecting this thread because I found some discrepancies between Import and ImportGui relative to file structure.
I am calling FreeCAD from a Python script to generate drawings in a server environment, without GUI, and I'd rather keep it so, without resorting to xvfb (because startup time and resources usage would increase exponentially).
Now, I want to export whatever drawing I am generating with a wrapping App::Part. I do this programmatically, with the following lines, where __objs__ is the list of Bodies I want to export:

Code: Select all

root = FreeCAD.ActiveDocument.addObject('App::Part', 'Root')
root.Label = 'RootLabel'
root.addObjects(__objs__)
Then, I do the following to export such "root" object as STEP:

Code: Select all

import Import
Import.export([root], "<path>.step"))
From which I get:
'RootLabel' is not a shape, export will be ignored.

Of course, the same exact command through ImportGui works perfectly:

Code: Select all

import ImportGui
ImportGui.export([root], "<path>.step"))
Is this a bug? Is it expected? If so, do you have any idea of a workaround?
Thank you,

Daniel
Post Reply