Object Rotation to vector

Object Rotation to vector

Anonymous
Not applicable
1,176 Views
2 Replies
Message 1 of 3

Object Rotation to vector

Anonymous
Not applicable

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.

 

Rotate.PNG

 

 

#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.

 

 

Accepted solutions (1)
1,177 Views
2 Replies
Replies (2)
Message 2 of 3

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

The root cause was the referenced coordinate system when you created sketch lines by the points in "model space". In fact, we think all inputs to create sketch lines are based on Sketch space. So you will find the end points of some sketch lines are not the same with the inputs you defined in the point list after they are created. 

 

The changes marked as red would correct the errors.

 

Thanks,

Marshal

 

#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(sketch.modelToSketchSpace(primitive_points[s[0]]), sketch.modelToSketchSpace(primitive_points[s[1]]))
     
        #create and rotate the cylinders        
        add_cylinders(rootComp)


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


Marshal Tu
Fusion Developer
>
Message 3 of 3

Anonymous
Not applicable

Thanks Marshall,

   Thats just what I was after. 

 

Sincerely, Paul.

0 Likes