Error with Rectangular Pattern on Inserted Component

Error with Rectangular Pattern on Inserted Component

Hyger
Explorer Explorer
515 Views
5 Replies
Message 1 of 6

Error with Rectangular Pattern on Inserted Component

Hyger
Explorer
Explorer

I was about to ask for help with my code, because i kept getting this error message

 

2.PNG

 

and i could remove it by editing the feature (rectangular pattern) and then immediately clicking ok

 

3.PNG

4.PNG

But then it occurred to me that maybe it's the same problem as in my previous post ->

https://forums.autodesk.com/t5/fusion-api-and-scripts/rigid-joint-between-an-external-inserted-compo...

 

that there is a problem with inserted components, so insted of making the pattern from the inserted component, i used (the "hack"/solution Jorge_Jaramillo suggested as a solution in my previous post) the parent component that i created for the inserted component and that fixed the error

 

Here is the code

 

app = adsk.core.Application.get()
ui  = app.userInterface
des = adsk.fusion.Design.cast(app.activeProduct)
root = adsk.fusion.Component.cast(des.rootComponent)

#Select a face to make sketch on
face = ui.selectEntity("Select a Planar Face", "PlanarFaces").entity

#Make sketch on selected face
sketch = root.sketches.add(face)

#Add line
lines = sketch.sketchCurves.sketchLines
line = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 200, 0))

#Add a sketch point a distance from each end
points = sketch.sketchPoints
sketchPoint = points.add(adsk.core.Point3D.create(0, 10, 0))

#Find components
exampleComponent = app.data.activeHub.dataProjects.item(0).rootFolder.dataFolders.itemByName("Autodesk Example").dataFiles.item(0)

#Insert Components (Using forum hack https://forums.autodesk.com/t5/fusion-api-and-scripts/rigid-joint-between-an-external-inserted-component-and-a-sketch/td-p/12941821)
instantiate = root.occurrences
forumHack = instantiate.addNewComponent(adsk.core.Matrix3D.create())
instantiateComponent = forumHack.component.occurrences.addByInsert(exampleComponent, adsk.core.Matrix3D.create(), True)
instantiateComponent.isGroundToParent = True

#Get origin points
originComponent = forumHack.component.originConstructionPoint

#Create joint geometry
joints = root.joints

jointPointComponent = adsk.fusion.JointGeometry.createByPoint(originComponent)
jointPointSketch = adsk.fusion.JointGeometry.createByPoint(sketchPoint)
joint = joints.createInput(jointPointComponent, jointPointSketch)

#Joint settings
joint.isFlipped = False
joint.setAsRigidJointMotion()

#Create joints
joints.add(joint)

#Rectrangular pattern inputs
rectangularPatternComponents = adsk.core.ObjectCollection.create()

##### ---->>  This will create the error  <<----- #####
#rectangularPatternComponents.add(forumHack) # This works
rectangularPatternComponents.add(instantiateComponent) # This doesn't

componentAmount = adsk.core.ValueInput.createByString('3')
patternDistance = adsk.core.ValueInput.createByString("500")
patternDistType = adsk.fusion.PatternDistanceType.ExtentPatternDistanceType

#Create rectangular pattern
rectangularPattern = root.features.rectangularPatternFeatures

rectangularPatternInput = rectangularPattern.createInput(rectangularPatternComponents, line, componentAmount, patternDistance, patternDistType)
rectangularPatternInput.setDirectionTwo(line, adsk.core.ValueInput.createByString('1'), adsk.core.ValueInput.createByString('0 mm'))

rectangularPattern.add(rectangularPatternInput)

 

 

maybe i'm doing something wrong?

if not how do i report this bug?

 

thanks in advance

0 Likes
516 Views
5 Replies
Replies (5)
Message 2 of 6

espablo
Enthusiast
Enthusiast

Hi
Did you find a solution? I just ran into the same problem and I'm running out of ideas. I also noticed that when the loaded component is in the rootComponent the program works fine, but if I move the loaded component to another component and try to make a rectanglePattern I get exactly the same result as you.

0 Likes
Message 3 of 6

BrianEkins
Mentor
Mentor

Here's a modified version of the original program. The change that matters is that I'm calling createForAssemblyContext to get the origin point in the context of the root component. This is often called a "proxy" of the real point. The user manual discusses this.

 

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = adsk.fusion.Component.cast(des.rootComponent)

        #Make sketch on selected face
        sketch = root.sketches.add(root.xYConstructionPlane)

        #Add line
        lines = sketch.sketchCurves.sketchLines
        line = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(0, 200, 0))

        #Add a sketch point a distance from each end
        points = sketch.sketchPoints
        sketchPoint = points.add(adsk.core.Point3D.create(0, 10, 0))

        #Find components
        project = app.data.activeHub.dataProjects.item(0)
        exampleComponent = project.rootFolder.dataFolders.itemByName("Autodesk Example").dataFiles.item(0)
        occ = root.occurrences.addByInsert(exampleComponent, adsk.core.Matrix3D.create(), True)

        #Get origin points
        originPoint = occ.component.originConstructionPoint

        #*** This is the main thing that was missing ***
        originPoint = originPoint.createForAssemblyContext(occ)

        #Create joint geometry
        joints = root.joints

        jointPointComponent = adsk.fusion.JointGeometry.createByPoint(originPoint)
        jointPointSketch = adsk.fusion.JointGeometry.createByPoint(sketchPoint)
        joint = joints.createInput(jointPointComponent, jointPointSketch)

        #Joint settings
        joint.isFlipped = False
        joint.setAsRigidJointMotion()

        #Create joints
        joints.add(joint)

        #Rectrangular pattern inputs
        rectangularPatternComponents = adsk.core.ObjectCollection.create()

        rectangularPatternComponents.add(occ)

        componentAmount = adsk.core.ValueInput.createByString('3')
        patternDistance = adsk.core.ValueInput.createByString("15")
        patternDistType = adsk.fusion.PatternDistanceType.ExtentPatternDistanceType

        #Create rectangular pattern
        rectangularPattern = root.features.rectangularPatternFeatures

        rectangularPatternInput = rectangularPattern.createInput(rectangularPatternComponents, line, componentAmount, patternDistance, patternDistType)
        rectangularPatternInput.setDirectionTwo(line, adsk.core.ValueInput.createByString('1'), adsk.core.ValueInput.createByString('0 mm'))

        rectangularPattern.add(rectangularPatternInput)
    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))

  

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 4 of 6

espablo
Enthusiast
Enthusiast

What I mean is that when I use the addByInsert method for the root, the rectangularPattern works fine. However, if I create a component and want to use the addByInsert method for it, the rectangularPattern no longer works fine. The effect is the same as in Hyger. I am attaching a program in which this effect occurs.

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # get active datafile
        actDataFile: adsk.core.DataFile = app.activeDocument.dataFile
        if not actDataFile:
            ui.messageBox("Save the document once.")
            return

        # get datafile ID
        actId = app.activeDocument.dataFile.id

        # get the unique data file
        dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        targetDatafile: adsk.core.DataFile = None
        dataFiles = dataFolder.dataFiles.asArray()
        for df in dataFiles:
            if df.id != actId and df.fileExtension == "f3d":
                targetDatafile = df
                break

        if not targetDatafile:
            ui.messageBox("Could not find the data file to import.")
            return

        instantiate = root.occurrences
        newComponent = instantiate.addNewComponent(adsk.core.Matrix3D.create())

        ##### ---->>  This will create the error  <<----- #####
        # targetComponent = root.occurrences  # This works
        targetComponent = newComponent.component.occurrences  # This doesn't
        ##### ---->>  This will create the error  <<----- #####

        instantiateComponent = targetComponent.addByInsert(
            targetDatafile, adsk.core.Matrix3D.create(), False
        )

        # Rectrangular pattern inputs
        rectangularPatternComponents = adsk.core.ObjectCollection.create()
        rectangularPatternComponents.add(instantiateComponent)

        componentAmount = adsk.core.ValueInput.createByString("3")
        patternDistance = adsk.core.ValueInput.createByString("500")
        patternDistType = adsk.fusion.PatternDistanceType.ExtentPatternDistanceType

        # Create rectangular pattern
        rectangularPattern = root.features.rectangularPatternFeatures

        rectangularPatternInput = rectangularPattern.createInput(
            rectangularPatternComponents,
            root.xConstructionAxis,
            componentAmount,
            patternDistance,
            patternDistType,
        )
        rectangularPatternInput.setDirectionTwo(
            root.yConstructionAxis,
            adsk.core.ValueInput.createByString("1"),
            adsk.core.ValueInput.createByString("0 mm"),
        )

        rectangularPattern.add(rectangularPatternInput)

    except:
        if ui:
            ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor

Here's a new version of your program where I added one line that creates a proxy of the occurrence. This fixes the issue you're having. It's line 48.

 

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        # get active datafile
        actDataFile: adsk.core.DataFile = app.activeDocument.dataFile
        if not actDataFile:
            ui.messageBox("Save the document once.")
            return

        # get datafile ID
        actId = app.activeDocument.dataFile.id

        # get the unique data file
        dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        targetDatafile: adsk.core.DataFile = None
        dataFiles = dataFolder.dataFiles.asArray()
        for df in dataFiles:
            if df.id != actId and df.fileExtension == "f3d":
                targetDatafile = df
                break

        if not targetDatafile:
            ui.messageBox("Could not find the data file to import.")
            return

        instantiate = root.occurrences
        newComponent = instantiate.addNewComponent(adsk.core.Matrix3D.create())

        ##### ---->>  This will create the error  <<----- #####
        # targetComponent = root.occurrences  # This works
        targetComponent = newComponent.component.occurrences  # This doesn't
        ##### ---->>  This will create the error  <<----- #####

        instantiateComponent = targetComponent.addByInsert(
            targetDatafile, adsk.core.Matrix3D.create(), False
        )

        #### --->> This line fixes it by creating a proxy of the ocurrence. <<---- #####
        instantiateComponent = instantiateComponent.createForAssemblyContext(newComponent)

        # Rectrangular pattern inputs
        rectangularPatternComponents = adsk.core.ObjectCollection.create()
        rectangularPatternComponents.add(instantiateComponent)

        componentAmount = adsk.core.ValueInput.createByString("3")
        patternDistance = adsk.core.ValueInput.createByString("500")
        patternDistType = adsk.fusion.PatternDistanceType.ExtentPatternDistanceType

        # Create rectangular pattern
        rectangularPattern = root.features.rectangularPatternFeatures

        rectangularPatternInput = rectangularPattern.createInput(
            rectangularPatternComponents,
            root.xConstructionAxis,
            componentAmount,
            patternDistance,
            patternDistType,
        )
        rectangularPatternInput.setDirectionTwo(
            root.yConstructionAxis,
            adsk.core.ValueInput.createByString("1"),
            adsk.core.ValueInput.createByString("0 mm"),
        )

        rectangularPattern.add(rectangularPatternInput)

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

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 6

espablo
Enthusiast
Enthusiast

Ha! It works. Although I still don't understand the mechanism of how this proxy works. But it works exactly as I want. Thank you

0 Likes