Import .dxf, extrude and export .step

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rchueca
Posts: 13
Joined: Fri Feb 19, 2021 9:32 pm

Import .dxf, extrude and export .step

Post by rchueca »

Hi, i am trying to find an example of this issue and i dont find any.

Is there any script that read 2D figure from .dxf file, extrude and export to a .step 3D file?

Please find .dxf file attached.

Many thanks.
Attachments
viga.dxf
(100.02 KiB) Downloaded 14 times
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Import .dxf, extrude and export .step

Post by onekk »

rchueca wrote: Thu Mar 23, 2023 11:26 am Hi, i am trying to find an example of this issue and i dont find any.

Is there any script that read 2D figure from .dxf file, extrude and export to a .step 3D file?

Please find .dxf file attached.

Many thanks.
2d to 3d how the system could guess what you want?

Automation is possible, reading mind not yet.

importing DXF is possible but it depends, as some DXF use "polyline" and is not too difficult think that a polyline should became FreeCAD wire, but as example to make a solid from a wire, wire must be closed and sometimes it is not.

Plus once you have a closed wire you could make it a face and then extrude it, how much thick? I doubt the DXF will contain this information.

Only to point out a couple of problem that could break a macro workflow.

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/
rchueca
Posts: 13
Joined: Fri Feb 19, 2021 9:32 pm

Re: Import .dxf, extrude and export .step

Post by rchueca »

Hello,

Sorry if my question was not correct. I am new in FreeCAD. I need to know if exits any example script to extrude a imported 2D CAD, if polyline is not valid, i can modify that to correct way in the original .dxf. To extrude, i could pass the thick value by parameter.

Thank you for your answer.
Regards.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Import .dxf, extrude and export .step

Post by onekk »

rchueca wrote: Fri Mar 24, 2023 6:57 am ...

Yout mileage may vary, as DXF is somewhat complicated to import, it depends heavily how it is made and what importer you use (there are at least two of them).

Once you have a closed wire it is trivial to make:

Code: Select all

face = Part.Face([wire])
solid = face.extrude(Vector(0.0, 0.0, 10.0))
Part.show(solid, "Solid")
Code above will work for a wire drawn in XY plane and extruded in Z positive direction.

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/
imm
Posts: 251
Joined: Wed Nov 03, 2021 1:00 pm

Re: Import .dxf, extrude and export .step

Post by imm »

This is a basic script that does what you are discussing....no bells and whistles....all pathes are hard coded and have to be adjusted.

Code: Select all

import FreeCADGui as Gui
import FreeCAD
from FreeCAD import Placement, Rotation, Vector
import importDXF
import importGui

adoc = FreeCAD.ActiveDocument

importDXF.insert(u"C:/Users/User/Downloads/freecad-samples/viga.dxf","Unnamed")

# -- select all
Gui.Selection.clearSelection()

for obj in adoc.Objects:
    Gui.Selection.addSelection("Unnamed",obj.Label)

# -- run draft upgrade to convert selection to a wire
Draft.upgrade(Gui.Selection.getSelection(),delete=True)
adoc.recompute()

# -- run draft upgrade to convert selection to a face
Draft.upgrade(Gui.Selection.getSelection(),delete=True)
adoc.recompute()

#face = Part.Face([wire])
# -- make a face and extrude
face = adoc.ActiveObject
solid = face.Shape.extrude(Vector(0.0,0.0,100.0))
Part.show(solid,"Solid")

# -- select object to export
Gui.Selection.clearSelection()
Gui.Selection.addSelection("Unnamed","Solid")

obj_lst = []
obj_lst.append(Gui.Selection.getSelection())
# -- do export as step
options = ImportGui.exportOptions(u"C:/Users/User/Downloads/freecad-samples/Unnamed-Solid.step")
ImportGui.export(obj_lst, u"C:/Users/User/Downloads/freecad-samples/Unnamed-Solid.step", options)
# -- cleanup temp list
del obj_lst

User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Import .dxf, extrude and export .step

Post by onekk »

imm wrote: Sun Apr 02, 2023 12:40 pm ...
Why use selections?

You are using python objects, you could retrieve them simply as objects list?

Selections are useful when you are operating interactively selecting objects and then process them.

If you have a file operate on his objects retrieved using.

Code: Select all

obj = Adoc.getObject()


Kind 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/
imm
Posts: 251
Joined: Wed Nov 03, 2021 1:00 pm

Re: Import .dxf, extrude and export .step

Post by imm »

I intentionally tried to mirror what occurs on the standard GUI interface.

It should make troubleshooting easier if something breaks.

Murphy's Law .... it will.
mario52
Veteran
Posts: 4674
Joined: Wed May 16, 2012 2:13 pm

Re: Import .dxf, extrude and export .step

Post by mario52 »

Hi

create face with DXF Macro_DXF_to_Face_and_Sketch Image

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Import .dxf, extrude and export .step

Post by onekk »

imm wrote: Sun Apr 02, 2023 2:06 pm ...
Not a clever move GUI mirror is usually too much verbose and not very pythonic and usually result in a overcomplicated code.

IMHO the worse way to learn how make FreeCAD scripts.

But is a personal opinion.

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/
imm
Posts: 251
Joined: Wed Nov 03, 2021 1:00 pm

Re: Import .dxf, extrude and export .step

Post by imm »

I expanded upon orginal post, but had to use an alternate export call....the commands detailed in the console were obviously not a complete picture of the 'behind-the scenes' processing. I could not get it to export if I tried changing the string associated with the export file directly.

You can start with no file open and Run the below code by pasting it into the console. It will open a dialog for the dxf input file.
It will then prompt for export options. I currently have it leave the document open.

It should also work as an independent macro.

If someone wants to pursue they can identify why the individual commands to export would not allow the export string to be modified away from 'standard'. I left the old code there but commented out so others could identify what did not work.

My alternate approach was to change the name of the active document to match the import filename (minus the .dxf extension) and call the
'Std_Export' command. This at least produces a unique export for each import file....script is below.

Code: Select all

import FreeCADGui as Gui
import FreeCAD as App
from FreeCAD import Placement, Rotation, Vector
import Draft
import importDXF
import ImportGui
import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *

def dlgGetFilename( fName=""):
    try:
        fName = QFileDialog.getOpenFileName(None,QString.fromLocal8Bit("Read a file dxf"),path,"*.dxf") # PyQt4
    except Exception:
        fName, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "Read a file txt", path,"*.dxf") #PySide
    return fName

#path = FreeCAD.ConfigGet("UserAppData")
path = u"C:/Users/User/Downloads/freecad-samples"


OpenName = dlgGetFilename()

if OpenName == "":
    App.Console.PrintMessage("Process aborted - File not Found."+"\n")
else:
    App.Console.PrintMessage("Importing "+OpenName+"\n")
    #try:                                                                      

    # -- set document name to match dxf name without extension
    adoc_name = OpenName.split("/")[-1]
    adoc_name = sName.replace(".dxf","")

    #adoc = FreeCAD.ActiveDocument
    adoc = App.newDocument(adoc_name)
    #adoc_name = adoc.Label
    
    #importDXF.insert(u"C:/Users/User/Downloads/freecad-samples/viga.dxf",adoc_name)
    importDXF.insert(OpenName,adoc_name)
    
    
    obj_sel = Gui.Selection
    
    # -- select all
    obj_sel.clearSelection()
    
    for obj in adoc.Objects:
        obj_sel.addSelection(adoc_name,obj.Label)
    
    # -- run draft upgrade to convert selection to a wire
    Draft.upgrade(obj_sel.getSelection(),delete=True)
    adoc.recompute()
    
    # -- run draft upgrade to convert selection to a face
    Draft.upgrade(obj_sel.getSelection(),delete=True)
    adoc.recompute()
    
    # -- make a face and extrude ... I chose arbitrary extrusion amount of 100 on z axis
    face = adoc.ActiveObject
    solid = face.Shape.extrude(Vector(0.0,0.0,100.0))
    Part.show(solid,"Solid")

    # -- adjust view to show extruded solid
    Gui.activeDocument().activeView().viewAxonometric()
    Gui.SendMsgToActiveView("ViewFit")
    
    # -- select object to export
    obj_sel.clearSelection()
    obj_sel.addSelection(adoc_name,"Solid")
    adoc.recompute()
    
    # -- do export
    Gui.runCommand('Std_Export',0)


    # obj_lst = []
    # obj_lst.append(Gui.Selection.getSelection())
    # -- do export as step
    # outputfilename = u"{}".format(OpenName.replace('.dxf','.step'))
    # options = ImportGui.exportOptions(outputfilename)
    # ImportGui.export(obj_lst, outputfilename, options)
    #options = ImportGui.exportOptions(u"C:/Users/User/Downloads/freecad-samples/Unnamed-Solid.step")
    #ImportGui.export(obj_lst, u"C:/Users/User/Downloads/freecad-samples/Unnamed-Solid.step", options)
    # -- cleanup temp list
    # del obj_lst
    
    #App.closeDocument(adoc_name)

    #except Exception:                                                         
    #    App.Console.PrintError("Error during processing of the file "+OpenName+"\n")       

output file is below
viga-Solid.step
(64.35 KiB) Downloaded 4 times
Post Reply