Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Construction Plane

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
dinocoglitore
1046 Views, 4 Replies

Construction Plane

Hi all, I’m trying to create a construction plane in a Component (COMP2).

I would like to create it starting from two lines that belong to two sketches that have been created in another component COMP1.

I receveid this error :

construction_planse_on_components.PNG

 

The construction is possible from the user interface:

construction_planse_on_components_2.PNG

 

So I think that there is some mistake in my code.

Thank you for the help.

Dino

 

Here is my code:

import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)    

        # define root component
        rootComp = design.rootComponent

        # create new components: COMP1 COMP2
        transform = adsk.core.Matrix3D.create()
        occ1 = rootComp.occurrences.addNewComponent(transform)
        comp1 = occ1.component
        comp1.name = "COMP1"
    
        occ2 = rootComp.occurrences.addNewComponent(transform)
        comp2 = occ2.component
        comp2.name = "COMP2"
        
        # create two line on COMP1
        # Create a new sketch on the  xy plane on COMP1
        sketches = comp1.sketches;
        xyPlane = rootComp.xYConstructionPlane
        sketch1 = sketches.add(xyPlane)

        xzPlane = rootComp.xZConstructionPlane
        sketch2 = sketches.add(xzPlane)

        # Draw two lines on COMP1
        lines1 = sketch1.sketchCurves.sketchLines
        lines2 = sketch2.sketchCurves.sketchLines
        
        line1 = lines1.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 0, 3))
        line2 = lines2.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 0, 3))

        # plane collection on the COMP2
        planes2 = comp2.constructionPlanes
        # Create construction plane input
        planeInput2 = planes2.createInput()

        # Add construction plane by two edges
        planeInput2.setByTwoEdges(line1, line2)
        plane = planes2.add(planeInput2)
        plane.name = "PLANE-COMP2"


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
4 REPLIES 4
Message 2 of 5
ekinsb
in reply to: dinocoglitore

This is an area of Fusion and the API that can sometimes be difficult to understand.  I've made a change to your code so that it now works but have also added some other code so that it can help to explain why this is more difficult than it would first appear.

 

 

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)    

        # define root component
        rootComp = design.rootComponent

        # create new components: COMP1 COMP2
        transform = adsk.core.Matrix3D.create()
        occ1 = rootComp.occurrences.addNewComponent(transform)
        comp1 = occ1.component
        comp1.name = "COMP1"
    
        occ2 = rootComp.occurrences.addNewComponent(transform)
        comp2 = occ2.component
        comp2.name = "COMP2"

        # Create a new occurrence referencing comp1.        
        transform.setCell(0, 3, 5)
        occ1_New = rootComp.occurrences.addExistingComponent(comp1, transform)
        
        # create two line on COMP1
        # Create a new sketch on the  xy plane on COMP1
        sketches = comp1.sketches;
        xyPlane = rootComp.xYConstructionPlane
        sketch1 = sketches.add(xyPlane)

        xzPlane = rootComp.xZConstructionPlane
        sketch2 = sketches.add(xzPlane)

        # Draw two lines on COMP1
        lines1 = sketch1.sketchCurves.sketchLines
        lines2 = sketch2.sketchCurves.sketchLines
        
        line1 = lines1.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 0, 3))
        line2 = lines2.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 0, 3))
        
        # Create proxies for the lines.
#        line1Proxy = line1.createForAssemblyContext(occ1)
#        line2Proxy = line2.createForAssemblyContext(occ1)
        line1Proxy = line1.createForAssemblyContext(occ1_New)
        line2Proxy = line2.createForAssemblyContext(occ1_New)

        # plane collection on the COMP2
        planes2 = comp2.constructionPlanes
        # Create construction plane input
        planeInput2 = planes2.createInput()

        # Add construction plane by two edges
        planeInput2.setByTwoEdges(line1Proxy, line2Proxy)
        #planeInput2.creationOccurrence = occ2
        plane = planes2.add(planeInput2)
        plane.name = "PLANE-COMP2"

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

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 5
ekinsb
in reply to: dinocoglitore

This is an area of Fusion and the API that can sometimes be difficult to understand.  I've made a change to your code so that it now works but have also added some other code so that it can help to explain why this is more difficult than it would first appear.  The change I made to the code is that after it creates the two new components, I create a new occurrence that references the existing COMP1.  Because both occurrences are referencing the same component, any changes made to that component are shown by both occurrences.  This is illustrated below where the two lines are draw in COMP1 and show up in both COMP1:1 and COMP1:2, so there are effectively four lines in the assembly although there are actually only the two in COMP1.

 

proxy1.png

 

In your original program you are referencing the two lines directly in the component COMP1, but Fusion needs more context than that to understand what you want to do.  For example, the horizontal line in the component shows up twice in the assembly so Fusion doesn't know which line you want.  What you need to do is create a "Proxy" of that line that represents a specific line.  What you're really doing is creating an object that understands the full path to the desired entity.  The path consists of occurrences.  In this case the path is only one level deep but it can be more in the case of multiple levels in an assembly.  In the code below it uses createForAssemblyContext method which creates a new SketchLine object that has additional context information associated with it.  It doesn't actually create a new entity in Fusion but creates a new API object with the additional path information.  The new SketchLine object knows that it's referencing the existing sketch line in COMP1 but relative to the occurrence COMP1:2.  Hopefully this makes sense.

 

import adsk.core, adsk.fusion, traceback

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)    

        # define root component
        rootComp = design.rootComponent

        # create new components: COMP1 COMP2
        transform = adsk.core.Matrix3D.create()
        occ1 = rootComp.occurrences.addNewComponent(transform)
        comp1 = occ1.component
        comp1.name = "COMP1"
    
        occ2 = rootComp.occurrences.addNewComponent(transform)
        comp2 = occ2.component
        comp2.name = "COMP2"

        # Create a new occurrence referencing comp1.        
        transform.setCell(0, 3, 5)
        occ1_New = rootComp.occurrences.addExistingComponent(comp1, transform)
        
        # create two line on COMP1
        # Create a new sketch on the  xy plane on COMP1
        sketches = comp1.sketches;
        xyPlane = rootComp.xYConstructionPlane
        sketch1 = sketches.add(xyPlane)

        xzPlane = rootComp.xZConstructionPlane
        sketch2 = sketches.add(xzPlane)

        # Draw two lines on COMP1
        lines1 = sketch1.sketchCurves.sketchLines
        lines2 = sketch2.sketchCurves.sketchLines
        
        line1 = lines1.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 0, 3))
        line2 = lines2.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 0, 3))
        
        # Create proxies for the lines.
#        line1Proxy = line1.createForAssemblyContext(occ1)
#        line2Proxy = line2.createForAssemblyContext(occ1)
        line1Proxy = line1.createForAssemblyContext(occ1_New)
        line2Proxy = line2.createForAssemblyContext(occ1_New)

        # plane collection on the COMP2
        planes2 = comp2.constructionPlanes
        # Create construction plane input
        planeInput2 = planes2.createInput()

        # Add construction plane by two edges
        planeInput2.setByTwoEdges(line1Proxy, line2Proxy)
        #planeInput2.creationOccurrence = occ2
        plane = planes2.add(planeInput2)
        plane.name = "PLANE-COMP2"

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

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 4 of 5
dinocoglitore
in reply to: ekinsb

Hi Brian, thanks for your reply. Your explanation is very clear. I’m agree that the argument is quite difficult and I’m not sure to have completely understood the concepts that are behind. I have read again the part of documentation that deal with Occurrences and with your example is now clearer. I’ve already used your code and it’s ok. Now I have to apply it to my script.

Some other examples about proxies, occurrences and components could be useful for the newbies like me.

Thank you very much

Dino

Message 5 of 5
dinocoglitore
in reply to: ekinsb

Hi Brian, I have another doubt about the problem you solved.

All that you have sayed refers only to sub-components and not to the root component. Isn’t it ?

So if I woudl like to use  something like this: rootComp_New = rootComp.occurrences.addExistingComponent(rootComp, transform)

where rootComp is the root Component, I make a mistake like this :

construction_planse_on_components_3.PNG

In this case I have to create sub components of root on which to work and duplicate with the addExistingComponent()

Is it true ?

Thanks

Dino

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report