Macros recipes/sv

Rotera vyn med 90°
Detta makro roterar den nuvarande vyn med 90° åt vänster. Fungerar endast om du är i toppvyn.

import math from pivy import coin cam = Gui.ActiveDocument.ActiveView.getCameraNode rot = coin.SbRotation rot.setValue(coin.SbVec3f(0,0,1),math.pi/2) nrot = cam.orientation.getValue * rot cam.orientation = nrot

Rutnätskopiering
Detta makro kopierar det valda objektet flera gånger på ett rutnät. Du kan definiera antalet rader, kolumner och avståndet mellan dem. Du behöver ha pyqt installerat.

import FreeCAD, FreeCADGui, Part from PyQt4 import QtGui,QtCore def proceed: try: u = (int(l1.text),float(l2.text)) v = (int(l3.text),float(l4.text)) except: FreeCAD.Console.PrintError("Fel inmatning! Endast siffror är tillåtna...\n") sel = FreeCADGui.Selection.getSelection if sel: sel = sel[0] name = sel.Name shape = sel.Shape for column in range(u[0]): for row in range(v[0]): if (column != 0) or (row != 0): delta = FreeCAD.Vector(column*u[1],row*v[1],0) newshape = sel.Shape newshape.translate(delta) newobject = FreeCAD.ActiveDocument.addObject("Part::Feature",name) newobject.Shape = newshape else: FreeCAD.Console.PrintError("Fel: Ett objekt måste väljas") hide def hide: dialog.hide dialog = QtGui.QDialog dialog.resize(200,300) dialog.setWindowTitle("Array") la = QtGui.QVBoxLayout(dialog) t1 = QtGui.QLabel("Antal kolumner") la.addWidget(t1) l1 = QtGui.QLineEdit la.addWidget(l1) t2 = QtGui.QLabel("Avstånd mellan kolumner") la.addWidget(t2) l2 = QtGui.QLineEdit la.addWidget(l2) t3 = QtGui.QLabel("Antal rader") la.addWidget(t3) l3 = QtGui.QLineEdit la.addWidget(l3) t4 = QtGui.QLabel("Avstånd mellan rader") la.addWidget(t4) l4 = QtGui.QLineEdit la.addWidget(l4) okbox = QtGui.QDialogButtonBox(dialog) okbox.setOrientation(QtCore.Qt.Horizontal) okbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) la.addWidget(okbox) QtCore.QObject.connect(okbox, QtCore.SIGNAL("accepted"), proceed) QtCore.QObject.connect(okbox, QtCore.SIGNAL("rejected"), hide) QtCore.QMetaObject.connectSlotsByName(dialog) dialog.show

Platta ut tråd
Detta makro plattar ut ritlinjer som inte är parallella (i Z-axeln) till deras median Z koordinat.

import FreeCAD obj = FreeCAD.ActiveDocument.ActiveObject z = 0 for p in obj.Points: z += p.z z = z/len(obj.Points) newpoints = [] for p in obj.Points: newppoints.append(FreeCAD.Vector(p.x,p.y,z)) obj.Points = newppoints

Konvertera Nät till Delar
Detta makro konverterar valda nät till delar. Det har en bred tolerans, så använd det bara med objekt som inte har några kurvor, annars kommer du att få konstiga resultat.

import FreeCAD,FreeCADGui,Mesh,Part,MeshPart for obj in FreeCADGui.Selection.getSelection: if "Mesh" in obj.PropertiesList: faces = [] mesh = obj.Mesh segments = mesh.getPlanes(0.01) # använd en ganska strikt tolerans här for i in segments: if len(i) > 0: # ett segment kan ha innerhål wires = MeshPart.wireFromSegment(mesh, i) 		    # vi antar att den externa gränsen är den som ryms i den största markeringslådan if len(wires) > 0: ext = None max_length = 0 for i in wires: if i.BoundBox.DiagonalLength > max_length: max_length = i.BoundBox.DiagonalLength ext = i 	       	wires.remove(ext) # alla interiöra trådar markerar ett hål och deras orientering måste reverseras, annars så misslyckas Part.Face for i in wires: i.reverse # försäkra dig om att de exteriöra trådarna kommer först i listan wires.insert(0, ext) faces.append(Part.Face(wires)) shell=Part.Compound(faces) solid = Part.Solid(Part.Shell(faces)) name = obj.Name FreeCAD.ActiveDocument.removeObject(name) FreeCAD.ActiveDocument.addObject("Part::Feature",name).Shape = solid

Förena tråd
Detta makro hittar alla oanslutna kanter och kopplar ihop dem med den närmaste oanslutna genom att använda en linje. Det har en matrisform i inmatningen ( [shape1,shape2,...]).

def findWires(edges): def verts(shape): return [shape.Vertexes[0].Point,shape.Vertexes[-1].Point] def group(shapes): shapesIn = shapes[:] pointTst = [] pointOut =[] for s in shapesIn : pointTst=pointTst+[s.Vertexes[0].Point] pointTst=pointTst+[s.Vertexes[-1].Point] print pointTst changed = False for s in shapesIn: if len(s.Vertexes) < 2: print "ett hörn, det är en cirkel, lägg bara till" else: for v in verts(s): twoDot=0 for vv in pointTst: if v == vv: twoDot=twoDot+1 if v==vv and twoDot==2 : changed = True print "hittade matchande hörn" break if twoDot<2: print "kunde inte hitta matchande hörn..." pointOut.append(v) print "Dots non connected", pointOut return(changed,pointOut) def joint(point): for p in range(len(point)/2) : print point deltI=Part.Vertex(100,100,100).Point pos=1 for pp in range(len(point)-1) : print "position:",pp+1 if len(point)-1>1: deltN=(point[0]-point[pp+1]) if deltN.Length<deltI.Length: deltI=deltN pos=pp+1 print "changement",pos else: pos=1 print "points a joindre",point[0],point[pos] if point[0]!=point[pos]: Part.show(Part.makePolygon([point[0],point[pos]])) else: print "WARNING les points ont la meme valeurs " point.pop(0) point.pop(pos-1) point=0 #to have a return normally void return(point) working = True edgeSet = edges result = group(edgeSet) working = result[0] edgeSet = result[1] joint(result[1]) return result[1]

Automatisk Ritning
Denna kod tillåter användaren att få vyer på sitt objekt i en ritning med 4 olika positioner(fram,topp,iso,höger). Behöver lite ändringar för att bli perfekt...

import FreeCAD, Part, Drawing if len(Gui.Selection.getSelectionEx)>1: App.Console.PrintError("Varning: Endast första punkten genereras") if len(Gui.Selection.getSelectionEx)==0: App.Console.PrintError("Varning: Du behöver välja en punkt") Piece=Gui.Selection.getSelectionEx[0] App.activeDocument.addObject('Drawing::FeaturePage','AutoDrawing') App.activeDocument.AutoDrawing.Template = App.getResourceDir+'Mod/Drawing/Templates/A3_Landscape.svg' DH=20 DL=30 L=Piece.Object.Shape.BoundBox.XMax H=Piece.Object.Shape.BoundBox.ZMax P=Piece.Object.Shape.BoundBox.YMax Sc=(400-3*DL)/(L+H) Sc2=(250-3*DH)/(P+H) if Sc>Sc2 : Sc=Sc2 TopX=DL+Sc*L FrontX=DL+Sc*L RightX=2*DL+Sc*L IsoX=2*DL+Sc*(L) TopY=DH+Sc*P RightY=DH+P*Sc FrontY=2*DH+Sc*(P+H) IsoY=2*DH+Sc*P print TopX,RightX,TopY,FrontY App.activeDocument.addObject('Drawing::FeatureViewPart','topView') App.activeDocument.topView.Source =Piece.Object App.activeDocument.topView.Direction = (0,0,1) App.activeDocument.topView.Rotation=180 App.activeDocument.topView.X = TopX App.activeDocument.topView.Y = TopY App.activeDocument.topView.ShowHiddenLines=True App.activeDocument.AutoDrawing.addObject(App.activeDocument.topView) App.activeDocument.topView.Scale = Sc App.activeDocument.addObject('Drawing::FeatureViewPart','FrontView') App.activeDocument.FrontView.Source =Piece.Object App.activeDocument.FrontView.Direction = (0,-1,0) App.activeDocument.FrontView.Rotation=90 App.activeDocument.FrontView.Scale = Sc App.activeDocument.FrontView.X = FrontX App.activeDocument.FrontView.Y = FrontY App.activeDocument.FrontView.ShowHiddenLines=True App.activeDocument.AutoDrawing.addObject(App.activeDocument.FrontView) App.activeDocument.addObject('Drawing::FeatureViewPart','RightView') App.activeDocument.RightView.Source =Piece.Object App.activeDocument.RightView.Direction = (1,0,0) App.activeDocument.RightView.Scale = Sc App.activeDocument.RightView.X = RightX App.activeDocument.RightView.Y = RightY App.activeDocument.RightView.ShowHiddenLines=True App.activeDocument.AutoDrawing.addObject(App.activeDocument.RightView) App.activeDocument.addObject('Drawing::FeatureViewPart','IsoView') App.activeDocument.IsoView.Source =Piece.Object App.activeDocument.IsoView.Direction = (1,1,1) App.activeDocument.IsoView.Rotation=60 App.activeDocument.IsoView.Scale = Sc*.6 App.activeDocument.IsoView.X = IsoX App.activeDocument.IsoView.Y = IsoY App.activeDocument.IsoView.ShowHiddenLines=True App.activeDocument.AutoDrawing.addObject(App.activeDocument.IsoView)
 * 1) Create topView
 * 1) Create FrontView
 * 1) Create RightView
 * 1) Create IsotView

Rita 2D funktion
Använd den för att rita en funktion som beskrivs av en "ekvation" [z=F(x)] (Z-X plan) Exemplet här genererar en parabol. Behöver definieras : F=variabel som används i funktionen, X=startvärde på x, Nb= antal steg, Z=funktionsuttryck med x ZZ=funktionsuttryck med xx

import FreeCAD, FreeCADGui, Part import math F=800 X=-500 Nb=10 Step=1000/Nb Y=0 for I in range(Nb): XX=X+Step Z=X*X/(4*F) ZZ=XX*XX/(4*F) if I==0: print "Le test est vrai !" nomme=Part.makeLine((X,Y,Z),(XX,Y,ZZ)) WWire=Part.Wire([nomme]) else : print "Le test est 2 !" nomme=Part.makeLine((X,Y,Z),(XX,Y,ZZ)) WWire=Part.Wire([WWire,nomme]) X=XX Part.show(WWire)