Exporting assembly to STEP format

Discussion about the development of the Assembly workbench.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
zzip
Posts: 3
Joined: Mon Feb 17, 2014 5:49 pm

Exporting assembly to STEP format

Post by zzip »

Hello guys,

I'm currently playing with branch jriegel/dev-assembly (build 3416), which compiles without any problems, and I'm facing a weird behavior when I try to export assemblies. I know it's in alpha state & all, but maybe it's just me doing it wrong or perhaps I've got my hand on an point that you'll be interested in... Anyway I've read somewhere that feedback is always good :)

When I...
- open the assembly workbench and it creates a new assembly for me
- "Import an existing component" and select a step file, and do it for, say 2 or 3 step files,
- add some constraints and manage to get some changes synced with the Gui
- hit CTRL+A in the objets tree
- and hit CTRL-E to export this assembly in STEP

... then all I get is a resulting STEP with the very components I've added before, but those components haven't moved, like if I've done no assembling work at all.
Some exporting formats _does work_, like "export to PDF" for example.

After some investigation :
- import from Part workbench with "ImportGui.Insert" and export from Assembly with ImportGui.export _works_, which means I get all my changes inside the exported STEP file. The thing is, components being not properly imported inside Assembly objects, I cannot use the constraints,
- Import and Export from Assembly workbench with AssemblyLib.ImportAssembly and ImportGui.export _don't work_: I can add constraints but can't get my changes exported inside a step file.

Looks like the mechanism behind AssemblyLib.ImportAssembly (which is Part.read and not Part.insert) is maybe not suited for a proper export in STEP?

Could be great to have some insight from some veterans here, and maybe I can help getting around this?

Thanks, cheers
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Exporting assembly to STEP format

Post by jriegel »

STEP export does not care about assembly structures. Thats not implemented yet....
Stop whining - start coding!
zzip
Posts: 3
Joined: Mon Feb 17, 2014 5:49 pm

Re: Exporting assembly to STEP format

Post by zzip »

That's what I thought, thanks for your reply.
Do you think it's something I could help you with somehow?
What would be roughly the way you would implement it?
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Exporting assembly to STEP format

Post by jriegel »

Since for Assembly STEP is a vital part, I already started a new Import/export line of code in the Import module. There are basically two possible approaches to that problem.

1st approach is to use the OCAF (OCC document model) translator and generate Assembly objects instead of Part objects. Unfortunately do OCC only transfer STEP objects/concepts it has objects for in its library. Mostly geometric stuff. Also no support for more specialized STEP stuff like STEP-NC or IFC....

There for I will go for the 2nd approach. I use the STEP-Code project python binding to load all high level objects from a STEP file, like Produkts, Persons and all kind of meta data. Interpret it with a python script, which makes it very flexible and easy to extend. After heaving the high level information I use the low level OCC translater to just get the (Part 42) shapes.

For Approach 1 you get faster results, but got most likely stuck very soon. Approach 2 I already started but got not much time for it lately.
Stop whining - start coding!
zzip
Posts: 3
Joined: Mon Feb 17, 2014 5:49 pm

Re: Exporting assembly to STEP format

Post by zzip »

Thanks for your insight! Your high-level-pythonic approach is indeed the best between the two.
I may try to implement the direct translator way, but just as a personal exercise ;)
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Exporting assembly to STEP format

Post by jriegel »

Why not, as more choices as better!
Stop whining - start coding!
galou_breizh
Posts: 436
Joined: Wed Sep 15, 2010 9:38 am

Re: Exporting assembly to STEP format

Post by galou_breizh »

I also neede this and wrote a small macro that takes all shapes of an assembly and copy them into a new document.

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui

doc = App.activeDocument()

# Get the list of features and their placement
feature_list = []
placement_list = []
label_list = []
for o in doc.Objects:
    if o.TypeId == 'PartDesign::Solid':
        feature_list.append(o)
        body = o.InList[0]
        part = body.InList[0]
        placement = part.Placement
        placement_list.append(placement)
        label_list.append(o.Label)

# Copy the features and place them
new_doc = App.newDocument()
fpl_list = zip(feature_list, placement_list, label_list)
for feature, placement, label in fpl_list:
    o = new_doc.addObject('Part::Feature', label)
    o.Shape = feature.Shape
    o.Placement = placement

Gui.SendMsgToActiveView("ViewFit")
Hope this helps,
Gaël
fred_stmk
Posts: 5
Joined: Fri May 15, 2015 10:07 pm

Re: Exporting assembly to STEP format

Post by fred_stmk »

Hi,

I use FreeCAD 0.15 and the above code did not work for me.

so I tried to debug and get this:
... extract any
  • Part::Feature
    Part::FeaturePython
... to a single
  • FreeCAD and
    STEP-File

in a subDirectory
"myExport"
in the Directory where your base FreeCAD File is stored

Code: Select all

# import FreeCAD as App
import os
import shutil
import FreeCAD

# import FreeCAD as App
# import FreeCADGui as Gui


print "Start"
doc = App.activeDocument()
# print doc.PropertiesList
print "document: " + doc.getPropertyByName('Label')
activeFileName = doc.getPropertyByName('FileName')
print "document: " + activeFileName


exportDir = os.path.dirname(activeFileName) + "/myExport"
print "exportDir: " + exportDir
#
if not os.path.exists(exportDir):
    os.makedirs(exportDir)

#
for filename in os.listdir(exportDir):
    filepath = os.path.join(exportDir, filename)
    try:
        shutil.rmtree(filepath)
    except OSError:
        os.remove(filepath)
#

# Get the list of features and their placement
feature_list = []
placement_list = []
label_list = []
#
print "\n\n -- STEP export ---\n\n"
for o in doc.Objects:
    # print "    o: " + o.TypeId + " / " + o.Label
    if o.TypeId == 'Part::Feature' or o.TypeId == 'Part::FeaturePython':
        print "    o: " + o.TypeId + " / " + o.Label
        feature_list.append(o)
        exportFileName = exportDir + "/" + o.Label + ".stp"
        print "    export: " + exportFileName
        #
        o.Shape.exportStep(exportFileName)
        #
        #print o.PropertiesList
        # body = o.getInList[0]
        # part = body.getInList[0]
        # placement = part.Placement
        # placement_list.append(placement)
        label_list.append(o.Label)
    else:
        print "    o: " + o.TypeId + " ... exception"
#    
#exit
#
#
#
print "\n\n -- FreeCAD export ---\n\n"
#
#
#
# new_doc = App.newDocument()
# fpl_list = zip(feature_list, placement_list, label_list)
# exportFileName = exportDir + "/" + label + ".stp"
# exportFileName = exportDir + "/complete.stp"
#
#for feature, placement, label in fpl_list:
fpl_list = zip(feature_list, label_list)
#
for feature, label in fpl_list:
    print "    export: " + label
    freeCADFileName = exportDir + "/" + label + ".FCStd"
    stepFileName = exportDir + "/" + label + "_b.stp"
    print "    export: " + freeCADFileName
    print "    export: " + stepFileName
        #
    newDoc = App.newDocument(label)
    o = newDoc.addObject('Part::Feature', label)
    o.Shape = feature.Shape
    App.getDocument(label).saveAs(freeCADFileName)
        #
        # o.Placement = placement
    Gui.SendMsgToActiveView("ViewFit")
    newDoc.recompute()
        #
    #o.Shape.exportStep(stepFileName)
        #
    App.closeDocument(label)
    

Gui.SendMsgToActiveView("ViewFit")
#
#
#
print "\n\n -- Done ! ---\n\n"
#
#
#
exit

cheers Manfred
Post Reply