using for loop with Sketch/Revolution

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
E Guerra
Posts: 8
Joined: Tue Sep 08, 2015 4:50 pm

using for loop with Sketch/Revolution

Post by E Guerra »

hello
trying to generate multiple sketches, that will be revolved into solids,
basically the sketch will be revolved represent a drill bit, the code places multiple drillings that will be positioned on the different faces on a block
right now calling App.activeDocument().addObject('Sketcher::SketchObject', 'Sketch') and then App.activeDocument().addObject("PartDesign::Revolution", "Revolution"), each time:
the first Sketch/Revolution seems ok, but get error msg: Failed to validate broken face
but the next Sketch/Revolution did not create a solid, and the error msg only displayed 'Error'
an empty sketch was created with name Sketch001
a Revolution was created with name Revolution001 and it has no sketch
the next iter of the loop, produced the same; no solid, the separate Sketch/Revolution where now labelled
Sketch002 and Revolution002; on the Revolution002, the error msg: No Sketch Linked

not sure how to change names as the for loop reads the file with data for drillings;
when manually adding sketches, the name will change from Sketch, to Sketch001 automatically, same for Revolution; how is this done?
tried changing the Sketch label, and the Revolution label before end of the loop
but cannot seem to find the correct solution
User avatar
wandererfan
Veteran
Posts: 6321
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: using for loop with Sketch/Revolution

Post by wandererfan »

E Guerra wrote:not sure how to change names as the for loop reads the file with data for drilling
You don't have to assign names. FreeCAD will add a suffix to make a unique name every time you add a new DocumentObject.

The "No sketch linked" means you haven't set the Sketch property of the Revolution by the time the Document gets recomputed. Your code looks something like this?

Code: Select all

for sketchData in sketchFile:
    newSketch = App.activeDocument().addObject('Sketcher::SketchObject', 'Sketch')
    ... #extract geometry,constraints,etc from sketchData
    newSketch.addGeometry(xxxx)
    newSketch.addConstraint(xxxx) 
    ...
    newRev = App.activeDocument().addObject("PartDesign::Revolution", "Revolution")
    newRev.Sketch = newSketch     #solves "no sketch link"
    newRev.ReferenceAxis = (newSketch,['V_Axis'])
    newRev.Angle = 360.0
    ...
E Guerra wrote:but get error msg: Failed to validate broken face
Hard to help with this one without seeing some code.

wf
E Guerra
Posts: 8
Joined: Tue Sep 08, 2015 4:50 pm

Re: using for loop with Sketch/Revolution

Post by E Guerra »

hi
thanks for your reply
changed the code, using Part.makePolygon:

row = Front,8.6,7.11,7.11,31.75 # read from a cvs file
tip = depth to tip of drill
pol = polyline of 5 vectors, making the sketch closed


tip = float(row[4]) + (tan(pi*(31.0/180.0)) * (float(row[1])/2.0))
dri, rad, dep = float(row[1]), float(row[1]) / 2.0, float(row[4])
xco, yco = float(row[2]), float(row[3]) # coordinates on block
pol = Part.makePolygon([
App.Vector(0.0, 0.0, 0.0),
App.Vector(0.0, tip, 0.0),
App.Vector(rad, dep, 0.0),
App.Vector(rad, 0.0, 0.0),
App.Vector(0.0, 0.0, 0.0)])
fac = Part.Face(pol)
rev = fac.revolve(App.Vector(0.0, 0.0, 0.0), App.Vector(0.0, 1.0, 0.0))
Part.show(rev)


at this point the solid represents a drill of length dep


# change name of drill, i.e.: Front,17.0,X: 25.00, Y: 37.5, 57.00 Deep
lbl = row[0] + ', ' + 'Drill: ' + row[1] + ', X: ' + row[2] + ', Y: ' + row[3] + ', ' + row[4] + ' Deep'
App.ActiveDocument.ActiveObject.Label = lbl


at this point the drill, is at 0.0, 0.0, 0.0 on front face
row[0] = face of block
self.moveto(face, x, y) is a function that moves Active object to new location


pvx, pvy, pvz, pax, pay, paz, paa = self.moveto(row[0], xco, yco)
App.ActiveDocument.ActiveObject.Placement = App.Placement(
App.Vector(pvx, pvy, pvz), App.Rotation(App.Vector(pax, pay, paz), paa))

[/color]

this code does work
thanks again
Post Reply