FreeCAD-Doc/localwiki/Macro_DXF_to_Face_and_Sketch-it.html
2018-07-19 18:47:02 -05:00

303 lines
14 KiB
HTML

<html><head><title>Macro DXF to Face and Sketch/it</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type='text/css' href='wiki.css' rel='stylesheet'></head><body><h1>Macro DXF to Face and Sketch/it</h1></div>
<div id="mw-content-text" lang="it" dir="ltr" class="mw-content-ltr"><hr/><div class="mw-parser-output"><table class="fcinfobox wikitable ct" width="100%" style="float: right; width: 230px; margin-left: 10px;">
<tr>
<td class="ctTitle">
<h3><span class="mw-headline" id="Facce_e_Schizzi_da_DXF"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Text-x-python.png" class="image"><img alt="Text-x-python.png" src="32px-Text-x-python.png" width="32" height="32" srcset="/wiki/images/2/2c/Text-x-python.png 1.5x" /></a> Facce e Schizzi da DXF</span></h3>
</td></tr>
<tr>
<th class="ctOdd">Descrizione
</th></tr>
<tr>
<td class="ctEven left">Questa macro crea faccia e schizzo da un file DXF
</td></tr>
<tr>
<th class="ctOdd">Autore
</th></tr>
<tr>
<td class="ctEven"><a href="https://www.freecadweb.org/wiki/index.php?title=User:Shoogen,_easyw-fc&amp;action=edit&amp;redlink=1" class="new" title="User:Shoogen, easyw-fc (page does not exist)">shoogen, easyw-fc</a>
</td></tr>
<tr>
<th class="ctOdd">Link
</th></tr>
<tr>
<td class="ctEven"><a href="https://www.freecadweb.org/wiki/index.php?title=Macros_recipes/it" title="Macros recipes/it">Esempi di macro</a><br /><a href="https://www.freecadweb.org/wiki/index.php?title=How_to_install_macros/it" title="How to install macros/it">Come installare le Macro</a><br /><a href="https://www.freecadweb.org/wiki/index.php?title=Customize_Toolbars/it" title="Customize Toolbars/it">Personalizzare la barra degli strumenti</a>
</td></tr>
<tr>
<th class="ctOdd">Versione
</th></tr>
<tr>
<td class="ctEven macro-version">21.0
</td></tr>
<tr>
<th class="ctOdd">Data ultima modifica
</th></tr>
<tr>
<td class="ctEven macro-date">2017-10-23
</td></tr>
<tr>
<th class="ctOdd">
</th></tr>
<tr>
<td class="ctToc"><br /><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1"><a href="#Facce_e_Schizzi_da_DXF"><span class="tocnumber">1</span> <span class="toctext">Facce e Schizzi da DXF</span></a></li>
<li class="toclevel-1 tocsection-1"><a href="#Descrizione"><span class="tocnumber">2</span> <span class="toctext">Descrizione</span></a></li>
<li class="toclevel-1 tocsection-2"><a href="#Uso"><span class="tocnumber">3</span> <span class="toctext">Uso</span></a></li>
<li class="toclevel-1 tocsection-3"><a href="#Opzioni"><span class="tocnumber">4</span> <span class="toctext">Opzioni</span></a></li>
<li class="toclevel-1 tocsection-4"><a href="#Script"><span class="tocnumber">5</span> <span class="toctext">Script</span></a></li>
<li class="toclevel-1 tocsection-5"><a href="#Link"><span class="tocnumber">6</span> <span class="toctext">Link</span></a></li>
</ul>
</div>
</td></tr>
</table>
<p><br />
</p>
<h2><span class="mw-headline" id="Descrizione">Descrizione</span></h2>
<p>Questa macro converte gli elementi selezionati di un DXF importato in facce e schizzi.
</p><p>Può essere utilizzato anche per convertire 'Shape2DView' in Schizzi e funziona anche se sono presenti B-Spline.
</p>
<h2><span class="mw-headline" id="Uso">Uso</span></h2>
<ul><li> Importare un file DXF</li>
<li> Selezionare gli elementi da processare</li>
<li> Avviare la macro, tutti gli oggetti selezionati vengono analizzati e trasformati in facce e in schizzi.</li></ul>
<h2><span class="mw-headline" id="Opzioni">Opzioni</span></h2>
<ul><li> Modificare create_face=True oppure create_sketch=True in False per generare solo un tipo di oggetti</li></ul>
<h2><span class="mw-headline" id="Script">Script</span></h2>
<p>Macro DXF to Face and Sketch.FCMacro
</p>
<pre>## 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) =&gt; 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):
##http://forum.freecadweb.org/viewtopic.php?f=12&amp;t=16336#p129468
##Discretizes the edge and returns a list of points.
##The function accepts keywords as argument:
##discretize(Number=n) =&gt; gives a list of 'n' equidistant points
##discretize(QuasiNumber=n) =&gt; gives a list of 'n' quasi equidistant points (is faster than the method above)
##discretize(Distance=d) =&gt; gives a list of equidistant points with distance 'd'
##discretize(Deflection=d) =&gt; gives a list of points with a maximum deflection 'd' to the edge
##discretize(QuasiDeflection=d) =&gt; gives a list of points with a maximum deflection 'd' to the edge (faster)
##discretize(Angular=a,Curvature=c,[Minimum=m]) =&gt; 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 &lt; 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):
##http://forum.freecadweb.org/viewtopic.php?f=12&amp;t=16336#p129468
##Discretizes the edge and returns a list of points.
##The function accepts keywords as argument:
##discretize(Number=n) =&gt; gives a list of 'n' equidistant points
##discretize(QuasiNumber=n) =&gt; gives a list of 'n' quasi equidistant points (is faster than the method above)
##discretize(Distance=d) =&gt; gives a list of equidistant points with distance 'd'
##discretize(Deflection=d) =&gt; gives a list of points with a maximum deflection 'd' to the edge
##discretize(QuasiDeflection=d) =&gt; gives a list of points with a maximum deflection 'd' to the edge (faster)
##discretize(Angular=a,Curvature=c,[Minimum=m]) =&gt; 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'&#160;% "dxf")
faceobj.Label = 'face_%s'&#160;% "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&#160;%s (%s)'&#160;% (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") </pre>
<h2><span class="mw-headline" id="Link">Link</span></h2>
<ul><li> Forum <a rel="nofollow" class="external text" href="http://forum.freecadweb.org/viewtopic.php?f=28&amp;t=16686">Creare uno sketch partendo da un file dxf importato</a></li>
<li> Esempi di macro <a rel="nofollow" class="external text" href="Macros_recipes.html">Macro Creating faces from a DXF file</a></li>
<li> Precedente versione <a href="https://www.freecadweb.org/wiki/index.php?title=Macro_Creating_faces_from_a_DXF_file/it" title="Macro Creating faces from a DXF file/it">Macro Creating faces from a DXF file</a></li></ul>
<div style="clear:both"></div>
</div>
</div>
</div><div class="printfooter">
Online version: "<a dir="ltr" href="https://www.freecadweb.org/wiki/index.php?title=Macro_DXF_to_Face_and_Sketch/it&amp;oldid=253187">http://www.freecadweb.org/wiki/index.php?title=Macro_DXF_to_Face_and_Sketch/it&amp;oldid=253187</a>"</div>
<div id="catlinks" class="catlinks" data-mw="interface"></div><div class="visualClear"></div>
</div>
</div>
<div id="mw-navigation">
<h2>Navigation menu</h2>
</body></html>