Macro DXF to Face and Sketch/fr


Text-x-python.png DXF en Face et Sketch

Description
Cette macro crée une face et un sketch à partir d'un fichier DXF.
Auteur
shoogen, easyw-fc
Liens
Recettes macros
Comment installer une macro
Comment ajouter une barre d'outils
Version
2.0
Date dernière modification
2017-10-23


Description

Cette macro crée une face et un sketch avec les éléments sélectionnés d'un fichier DXF.

Utilisation

Options

Script

Macro DXF to Face and Sketch.FCMacro

##    Select the items to be processed
##    Launch the macro, all selected objects are analyzed and transformed into faces and sketches.
##    change create_face=True or create_sketch=True to False to generate only one item
## todo: fix closed curves for bsplines method

import FreeCAD,Part

import sys, os
#sys.path.append('C:\Cad\Progetti_K\3D-FreeCad-tools/')
import FreeCAD,Part,OpenSCAD2Dgeom
import Draft
import PySide
from PySide import QtGui, QtCore


import DraftGeomUtils
import Draft

create_face=True
create_sketch=True

def clear_console():
    #clearing previous messages
    mw=FreeCADGui.getMainWindow()
    c=mw.findChild(QtGui.QPlainTextEdit, "Python console")
    c.clear()
    r=mw.findChild(QtGui.QTextEdit, "Report view")
    r.clear()


#creating face from dxf
doc = App.ActiveDocument
clear_console()

if FreeCADGui.Selection.getSelection():

    try:
        edges=sum((obj.Shape.Edges for obj in \
        FreeCADGui.Selection.getSelection() if hasattr(obj,'Shape')),[])
        for edge in edges:
            print "geomType ",DraftGeomUtils.geomType(edge)
        #face = OpenSCAD2Dgeom.edgestofaces(edges)
        face = OpenSCAD2Dgeom.edgestofaces(edges)
        face.check() # reports errors
        face.fix(0,0,0)
        faceobj = doc.addObject('Part::Feature','face_%s' % "dxf")
        faceobj.Label = 'face_%s' % "dxf"
        faceobj.Shape = face
        for obj in FreeCADGui.Selection.getSelection():
            Gui.ActiveDocument.getObject(obj.Name).Visibility=False   
        #creating sketch from face from dxf
        #wires,_faces = Draft.downgrade(FreeCADGui.Selection.getSelection(),delete=False)
        if create_sketch:
            if create_face:
                wires,_faces = Draft.downgrade(faceobj,delete=False)
            else:
                wires,_faces = Draft.downgrade(faceobj,delete=True)
            sketch = Draft.makeSketch(wires[0:1])
            sketch.Label = "Sketch_dxf"
            for wire in wires[1:]:
                Draft.makeSketch([wire],addTo=sketch)
            for wire in wires:
                FreeCAD.ActiveDocument.removeObject(wire.Name)
    except Part.OCCError: # Exception: #
        FreeCAD.Console.PrintError('Error in dxf %s (%s)' % (faceobj.Name,faceobj.Label)+"\n")
else:
    #FreeCAD.Console.PrintError("Select elements from dxf imported file\n")
    FreeCAD.Console.PrintWarning("Select elements from dxf imported file\n") 

Liens

Online version: "http://www.freecadweb.org/wiki/index.php?title=Macro_DXF_to_Face_and_Sketch/fr&oldid=242157"

Navigation menu