Message 1 of 3

Not applicable
09-23-2017
12:28 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good evening...
I'm trying to rotate an object to align with a vector similar what was requested in [this post], but I don't feel the vector part was answered. I've sketched 4 lines representing what I want the final rotation to be and created cylinder components to rotate to those lines (vectors). From the image below however you can see that the rotation is incorrect, only 2 of the 4 lines are rotated correctly.
#Author- #Description- import adsk.core, adsk.fusion, adsk.cam, traceback _app = adsk.core.Application.get() _product = _app.activeProduct _design = adsk.fusion.Design.cast(_product) primitive_points = [] points = [[0,0,0],[1,1,1],[-1,1,1],[1,-1,1],[-1,-1,1]] for p in points: primitive_points.append(adsk.core.Point3D.create(p[0], p[1], p[2])) #Pairs of points that make vectors strut_points = [[0,1],[0,2],[0,3],[0,4]] def createNewComponent(parent, name): allOccs = parent.occurrences newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create()) newOcc.component.name = name return newOcc.component def add_cylinders(parent): # Create a component to group struts of a given length strut_comp = createNewComponent(parent, 'Cylinder Group') #Create a cylinder component for a given length comp = createNewComponent(strut_comp, 'Strut') sketches = comp.sketches sketch = sketches.add(strut_comp.xZConstructionPlane) sketch.name = "circle" # Draw a circle. circles = sketch.sketchCurves.sketchCircles circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 0.1) # Get the profile defined by half of the circle. prof = sketch.profiles.item(0) # Create an extrusion input to be able to define the input needed for a revolution # while specifying the profile and that a new component is to be created extrudes = comp.features.extrudeFeatures distance = adsk.core.ValueInput.createByString("10 cm") extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # Create copies of the cylinder component and rotate them to align with the angled vectors for strut in strut_points: #Create all the strut copies from the master component allOccs = strut_comp.occurrences transform = adsk.core.Matrix3D.create()
vec_from = adsk.core.Vector3D.create(0, 1, 0) # Y axis - straight up vec_to = primitive_points[strut[0]].vectorTo(primitive_points[strut[1]]) # Vector to rotate to transform.setToRotateTo(vec_from, vec_to) allOccs.addExistingComponent(comp, transform) def run(context): ui = None try: ui = _app.userInterface #get the root component of the active design rootComp = _design.rootComponent # create a sketch sketches = rootComp.sketches sketch = sketches.add(rootComp.xZConstructionPlane) # Get the SketchLines collection from an existing sketch. lines = sketch.sketchCurves.sketchLines #Create some sketch lines at the angle we want to rotate the cylinders to for s in strut_points: lines.addByTwoPoints(primitive_points[s[0]], primitive_points[s[1]]) #create and rotate the cylinders add_cylinders(rootComp) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
So the question is what am I missing?
Sincerely, Paul.
Solved! Go to Solution.