FreeCAD Logo FreeCAD 1.0
  • Anglais Afrikaans Arabe Biélorusse Catalan Tchèque Allemand Grec Espagnol Espagnol Basque Finnois Philippin Français Galicien Croate Hongrois Indonésien Italien Japonais Kabyle Coréen Lituanien Néerlandais Norvégien classique Polonais Portugais Portugais Roumain Russe Slovaque Slovène Serbe Suédois Turc Ukrainien Valencien Vietnamien Chinois Chinois
  • Fonctions
  • Télécharger
  • Blog
  • Documentation
    Index de la documentation Premiers pas Documentation pour les utilisateurs Manuel de FreeCAD Documentation des ateliers Documentation sur le codage en Python Documentation pour les développeurs Tutoriels Foire aux questions Politique de confidentialité À propos de FreeCAD
  • Contribuer
    Comment aider Sponsor Signaler un bogue Faire une demande de modification (PR) Emplois et financements Guide pour les contributions Manuel pour les développeurs Traductions
  • Communauté
    Code de conduite Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendrier
  • ♥ Donate

Donate

$
Informations SEPA
Veuillez configurer votre virement bancaire SEPA pour:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Agence bancaire: BNP Paribas Fortis
Adresse: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

DXF to Face and Sketch

Description
Cette macro crée une face et un sketch à partir d'un fichier DXF.

Version macro : 2.0
Date dernière modification : 2017-10-23
Version FreeCAD : Toutes
Téléchargement : Icône de la barre d'outils
Auteur: shoogen, easyw-fc
Auteur
shoogen, easyw-fc
Téléchargement
Icône de la barre d'outils
Liens
Page des macros
Comment installer une macro
Comment créer une barre d'outils
Version Macro
2.0
Dernière modification
2017-10-23
Version(s) FreeCAD
Toutes
Raccourci clavier
None
Voir aussi
None

Description

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

Il peut également être utilisé pour convertir 'Shape2DView' en Sketch et cela fonctionne également si des BSPlines sont présentes.

Utilisation

  • Importez un fichier DXF
  • Sélectionnez les éléments à convertir
  • Lancez la macro, les éléments sélectionnés sont analysés et convertis en faces et en sketch (pour obtenir une face le fil doit être fermé).

Options

  • Modifiez create_face=True ou create_sketch=True en False pour généré un seul objet

Script

Icône de la barre d'outils


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,Path
from PathScripts import PathUtils

import sys, os
#sys.path.append('C:\Cad\Progetti_K\3D-FreeCad-tools/')
#sys.path.append(os.path.realpath(__file__)) #workaround to test OpenSCAD2DgeomMau
#import OpenSCAD2DgeomMau #OpenSCAD2Dgeom
#reload(OpenSCAD2DgeomMau)
import OpenSCAD2Dgeom

import PySide
from PySide import QtGui, QtCore


import DraftGeomUtils
import Draft
from DraftGeomUtils import geomType

create_face=True
create_sketch=True
dqd = 0.02 #discretize(QuasiDeflection=d) => gives a list of points with a maximum deflection 'd' to the edge (faster)


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()

def shape2Sketch(shape):
    # replace shape.edges with arcs
    newEdges = PathUtils.cleanedges(shape.Edges,0.1)
    wire = Part.Wire([Part.Edge(i) for i in newEdges])
    sketch = Draft.makeSketch(wire,autoconstraints=True,delete=True)
    return sketch

def Discretize(shape, addToName=None):
    ##https://forum.freecad.org/viewtopic.php?f=12&t=16336#p129468
    ##Discretizes the edge and returns a list of points.
    ##The function accepts keywords as argument:
    ##discretize(Number=n) => gives a list of 'n' equidistant points
    ##discretize(QuasiNumber=n) => gives a list of 'n' quasi equidistant points (is faster than the method above)
    ##discretize(Distance=d) => gives a list of equidistant points with distance 'd'
    ##discretize(Deflection=d) => gives a list of points with a maximum deflection 'd' to the edge
    ##discretize(QuasiDeflection=d) => gives a list of points with a maximum deflection 'd' to the edge (faster)
    ##discretize(Angular=a,Curvature=c,[Minimum=m]) => gives a list of points with an angular deflection of 'a'
    ##and a curvature deflection of 'c'. Optionally a minimum number of points
    ##can be set which by default is set to 2. 

    global dqd

    # lng=(abs(FreeCAD.ActiveDocument.getObject(skt_name).Shape.BoundBox.XLength)+abs(FreeCAD.ActiveDocument.getObject(skt_name).Shape.BoundBox.YLength))
    # dv=int(dvm*lng) #discretize auto setting
    # #dv=int(0.5*lng) #discretize auto setting COARSE for testing
    # #print( dv)
    # if dv < 20:
    #     dv=20
    #b=FreeCAD.ActiveDocument.getObject(skt_name)
    #l=b.Shape.copy().discretize(dv)
    #l=b.Shape.copy().discretize(QuasiDeflection=0.02)
    l=shape.copy().discretize(QuasiDeflection=dqd)
    f=Part.makePolygon(l)
    Part.show(f)
    sh_name=FreeCAD.ActiveDocument.ActiveObject.Name
    if addToName is None:
        Draft.makeSketch(FreeCAD.ActiveDocument.ActiveObject,autoconstraints=True)
    else:
        Draft.makeSketch(FreeCAD.ActiveDocument.ActiveObject,autoconstraints=True, addTo=addToName)
    s_name=FreeCAD.ActiveDocument.ActiveObject.Name
    FreeCAD.ActiveDocument.removeObject(sh_name)
    #FreeCAD.ActiveDocument.removeObject(skt_name)
    FreeCAD.ActiveDocument.recompute() 
    return s_name
    #stop

def WireDiscretize(wire):
    ##https://forum.freecad.org/viewtopic.php?f=12&t=16336#p129468
    ##Discretizes the edge and returns a list of points.
    ##The function accepts keywords as argument:
    ##discretize(Number=n) => gives a list of 'n' equidistant points
    ##discretize(QuasiNumber=n) => gives a list of 'n' quasi equidistant points (is faster than the method above)
    ##discretize(Distance=d) => gives a list of equidistant points with distance 'd'
    ##discretize(Deflection=d) => gives a list of points with a maximum deflection 'd' to the edge
    ##discretize(QuasiDeflection=d) => gives a list of points with a maximum deflection 'd' to the edge (faster)
    ##discretize(Angular=a,Curvature=c,[Minimum=m]) => gives a list of points with an angular deflection of 'a'
    ##and a curvature deflection of 'c'. Optionally a minimum number of points
    ##can be set which by default is set to 2. 

    global dqd

    l=wire.copy().discretize(QuasiDeflection=dqd)
    f=Part.makePolygon(l)
    Part.show(f)
    sh_name=FreeCAD.ActiveDocument.ActiveObject.Name
    FreeCAD.ActiveDocument.recompute() 
    
    return sh_name
    #stop
    
    
#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 = OpenSCAD2DgeomMau.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)
            if 0: #try:
                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)
            else: #except:
                FreeCAD.Console.PrintWarning("\nConverting Bezier curves to Arcs\n")
                #FreeCAD.ActiveDocument.removeObject(FreeCAD.ActiveDocument.ActiveObject.Name)
                
                #print()
                sketchesList=[]
                #for wire in wires:
                #    for e in wire.Shape.Edges:
                #        if geomType(e) == "BSplineCurve" or geomType(e) == "BezierCurve":
                #        #if 'spline' in e.Curve:
                #            sk_name = Discretize(e)
                #            sketchesList.append(sk_name)
                #        else:
                #            sketch = Draft.makeSketch(Part.Wire(e))
                #            sketchesList.append(sketch.Name)
                #print( sketchesList)
                
                newShapeList = []
                newShapes = []
                for wire in wires:
                    for e in wire.Shape.Edges:
                        if geomType(e) == "BSplineCurve" or geomType(e) == "BezierCurve":
                        #if 'spline' in e.Curve:
                            w = Part.Wire(e)
                            w_name =WireDiscretize(w)
                            newShapeList.append(w_name)
                            wn=FreeCAD.ActiveDocument.getObject(w_name)
                            newShapes.append(wn)
                        else:
                            w = Part.Wire(e)
                            Part.show(w)
                            newShapes.append(w)
                            w_name = FreeCAD.ActiveDocument.ActiveObject.Name
                            newShapeList.append(w_name)
                            
                #print( newShapes)
                sketch = Draft.makeSketch(newShapes[0])
                FreeCAD.ActiveDocument.ActiveObject.Label="Sketch_dxf"
                for w in newShapes[1:]:
                    Draft.makeSketch([w],addTo=sketch)    
                for wire in wires:
                    FreeCAD.ActiveDocument.removeObject(wire.Name)
                for w_name in newShapeList:
                    FreeCAD.ActiveDocument.removeObject(w_name)
                
                #stop
                #WireDiscretize(wire)
                
                
                #stop
                #sk_name=FreeCAD.ActiveDocument.ActiveObject.Name
                #skObj=FreeCAD.ActiveDocument.getObject(sk_name)
                #for wire in wires[1:]:
                #    Discretize(wire.Shape,skObj)
                    
                #for wire in wires:
                #    FreeCAD.ActiveDocument.removeObject(wire.Name)
                    
                #for wire in wires[1:]:
                #    Discretize(wire.Shape,sketch)
                #for wire in wires:
                #    FreeCAD.ActiveDocument.removeObject(wire.Name)
                #for wire in wires:
                #    FreeCAD.ActiveDocument.removeObject(wire.Name)
                #sketch = shape2Sketch(face)
                #sketch = Discretize(FreeCAD.ActiveDocument.getObject(faceobj.Name).Shape)
                #sketch = Discretize(FreeCAD.ActiveDocument.getObject(f1.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

  • Forum Creare uno sketch partendo da un file dxf importato
  • Macros_recipes Macro Creating faces from a DXF file
  • Macro d'origine Macro Creating faces from a DXF file

Cette page est extraite de https://wiki.freecad.org/Macro_DXF_to_Face_and_Sketch

Contactez-nous !
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© L'équipe FreeCAD. Crédits des images de la page d'accueil (de haut en bas) : ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

Ce projet est soutenu par : , KiCad Services Corp. et autres parrains

GitHubAméliorer cette page sur GitHub