<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Object Rotation to vector in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7403875#M17559</link>
    <description>&lt;P&gt;Good evening...&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I'm trying to rotate an object to align with a vector similar what was requested in [&lt;A href="https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-rotate-a-body-from-python-script/td-p/6069337" target="_blank"&gt;this post&lt;/A&gt;], 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Rotate.PNG" style="width: 705px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/404698iBA4719A1BC8CC459/image-size/large?v=v2&amp;amp;px=999" role="button" title="Rotate.PNG" alt="Rotate.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#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()&lt;BR /&gt;&lt;BR /&gt;        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()))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is what am I missing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely, Paul.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 23 Sep 2017 19:31:00 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2017-09-23T19:31:00Z</dc:date>
    <item>
      <title>Object Rotation to vector</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7403875#M17559</link>
      <description>&lt;P&gt;Good evening...&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I'm trying to rotate an object to align with a vector similar what was requested in [&lt;A href="https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-rotate-a-body-from-python-script/td-p/6069337" target="_blank"&gt;this post&lt;/A&gt;], 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Rotate.PNG" style="width: 705px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/404698iBA4719A1BC8CC459/image-size/large?v=v2&amp;amp;px=999" role="button" title="Rotate.PNG" alt="Rotate.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#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()&lt;BR /&gt;&lt;BR /&gt;        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()))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the question is what am I missing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely, Paul.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Sep 2017 19:31:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7403875#M17559</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-09-23T19:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Object Rotation to vector</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7405176#M17560</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The root cause&amp;nbsp;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.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The changes marked as red would&amp;nbsp;correct the errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Marshal&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;#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:
            &lt;FONT color="#FF0000"&gt;lines.addByTwoPoints(sketch.modelToSketchSpace(primitive_points[s[0]]), sketch.modelToSketchSpace(primitive_points[s[1]]))&lt;/FONT&gt;
     
        #create and rotate the cylinders        
        add_cylinders(rootComp)


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Sep 2017 03:24:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7405176#M17560</guid>
      <dc:creator>marshaltu</dc:creator>
      <dc:date>2017-09-25T03:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: Object Rotation to vector</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7406140#M17561</link>
      <description>&lt;P&gt;Thanks Marshall,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Thats just what I was after.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely, Paul.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2017 12:10:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/object-rotation-to-vector/m-p/7406140#M17561</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-09-25T12:10:29Z</dc:date>
    </item>
  </channel>
</rss>

