python api rectangular pattern get warning: Reference Failures, Failed to get owner occurrence transform

python api rectangular pattern get warning: Reference Failures, Failed to get owner occurrence transform

mr.liu0518
Observer Observer
618 Views
3 Replies
Message 1 of 4

python api rectangular pattern get warning: Reference Failures, Failed to get owner occurrence transform

mr.liu0518
Observer
Observer

I am trying to make a script for fusion360 to generate scaffolding(some tubes pattern with component structure); But when the final step(rectangular pattern) is done I got the warning: Reference Failures, Failed to get owner occurrence transform; I also create a simple test script trying to identify the problem, and I find that the issues only happened when the activeComponent is not the rootComponent. I need some advice to figure out the problem and avoid this.

import adsk.core, adsk.fusion, adsk.cam, traceback


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

        # get the active component of the design
        activeComp = design.activeComponent

        # create a new sub component
        subOccs = activeComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        subComp = subOccs.component
        subComp.name = "subComp"

        # draw a circle in the new component
        sketches = subComp.sketches
        xyPlane = subComp.xYConstructionPlane
        sketche = sketches.add(xyPlane)
        sketche.sketchCurves.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(0, 0, 0), 1
        )
        profile = sketche.profiles.item(0)

        # create an extrusion
        extInput = subComp.features.extrudeFeatures.createInput(
            profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation
        )
        distance = adsk.core.ValueInput.createByReal(1)
        extInput.setDistanceExtent(False, distance)
        subComp.features.extrudeFeatures.add(extInput)

        # retangle pattern the sub component
        inputEntities = adsk.core.ObjectCollection.create()
        inputEntities.add(subOccs)
        quantityOne = adsk.core.ValueInput.createByReal(2)
        quantityTwo = adsk.core.ValueInput.createByReal(2)
        distanceOne = adsk.core.ValueInput.createByReal(2)
        distanceTwo = adsk.core.ValueInput.createByReal(2)
        rectangPatternFeats = subComp.features.rectangularPatternFeatures
        rectangPatternFeatInput = rectangPatternFeats.createInput(
            inputEntities,
            subComp.zConstructionAxis,
            quantityOne,
            distanceOne,
            adsk.fusion.PatternDistanceType.SpacingPatternDistanceType,
        )
        rectangPatternFeatInput.setDirectionTwo(
            subComp.xConstructionAxis, quantityTwo, distanceTwo
        )
        rectangPatternFeats.add(rectangPatternFeatInput)

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

 

0 Likes
619 Views
3 Replies
Replies (3)
Message 2 of 4

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

I believe your problem is in line #43:

rectangPatternFeats = subComp.features.rectangularPatternFeatures

Shouldn't you use a different component to built the feature input other than the one you want to replicate with the pattern?  ActiveComp.features.rectangularPatternFeatures should be used instead?

 

I couldn't test it right now, because I'm outside of the office.  Maybe later I can give a try.

 

Regards,

Jorge Jaramillo

Software Engineer

 

0 Likes
Message 3 of 4

Jorge_Jaramillo
Collaborator
Collaborator

Hi @mr.liu0518 ,

 

I was able to make it run without warnings having root or another component active.

The problem was to reference items of the component (z and x construction axes) that is being replicated, and the lack of using the occurrence proxy if the target component is not the root component.

 

This is the code with some remarks with the changes:

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

        # get the active component of the design
        activeComp = design.activeComponent
        activeOccu = design.activeOccurrence # <== added

        # create a new sub component
        subOccs = activeComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        subComp = subOccs.component
        subComp.name = "subComp"

        # draw a circle in the new component
        sketches = subComp.sketches
        xyPlane = subComp.xYConstructionPlane
        sketche = sketches.add(xyPlane)
        sketche.sketchCurves.sketchCircles.addByCenterRadius(
            adsk.core.Point3D.create(0, 0, 0), 1
        )
        profile = sketche.profiles.item(0)

        # create an extrusion
        extInput = subComp.features.extrudeFeatures.createInput(
            profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation
        )
        distance = adsk.core.ValueInput.createByReal(1)
        extInput.setDistanceExtent(False, distance)
        subComp.features.extrudeFeatures.add(extInput)

        # retangle pattern the sub component
        inputEntities = adsk.core.ObjectCollection.create()
        if activeOccu:  # <== if added
            inputEntities.add(subOccs.createForAssemblyContext(activeOccu)) # <== use occurrence proxy if not from root
        else:
            inputEntities.add(subOccs)
        quantityOne = adsk.core.ValueInput.createByReal(2)
        quantityTwo = adsk.core.ValueInput.createByReal(2)
        distanceOne = adsk.core.ValueInput.createByReal(2)
        distanceTwo = adsk.core.ValueInput.createByReal(2)
        # rectangPatternFeats = subComp.features.rectangularPatternFeatures
        rectangPatternFeats = activeComp.features.rectangularPatternFeatures  # <== replace previous line
        rectangPatternFeatInput = rectangPatternFeats.createInput(
            inputEntities,
            # subComp.zConstructionAxis,
            activeComp.zConstructionAxis, # <== replace previous line
            quantityOne,
            distanceOne,
            adsk.fusion.PatternDistanceType.SpacingPatternDistanceType,
        )
        rectangPatternFeatInput.setDirectionTwo(
            # subComp.xConstructionAxis, quantityTwo, distanceTwo
            activeComp.xConstructionAxis, quantityTwo, distanceTwo # <== replace previous line
        )
        rectangPatternFeats.add(rectangPatternFeatInput)
    except:
        app.log("Failed:\n{}".format(traceback.format_exc()))

 

Regards,

Jorge Jaramillo

Software Engineer

 

0 Likes
Message 4 of 4

mr.liu0518
Observer
Observer

Yes, the rectangular pattern should apply to the parent component instead of the one I want to replicate with the pattern. I tried your code and it works properly, but I still have no idea when to use the occurrence proxy. I try the approach below and it still gets the warning when the active component is not the root component. 

import adsk.core, adsk.fusion, adsk.cam, traceback


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

        # get the active component and create sub component
        activeComp = design.activeComponent
        subOccu = activeComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        subComp = subOccu.component

        # create a sub_sub_component inside the previous created sub_component
        sub_subOccu = subComp.occurrences.addNewComponent(adsk.core.Matrix3D.create())
        # sub_subComp = sub_subOccu.component

        # rectangularPatterns
        inputEntities = adsk.core.ObjectCollection.create()
        inputEntities.add(sub_subOccu.createForAssemblyContext(subOccu))
        quantityOne = adsk.core.ValueInput.createByReal(4)
        distanceOne = adsk.core.ValueInput.createByReal(40)

        rectangularPatterns = subComp.features.rectangularPatternFeatures
        rectangularPatternInput = rectangularPatterns.createInput(
            inputEntities,
            subComp.zConstructionAxis,
            quantityOne,
            distanceOne,
            adsk.fusion.PatternDistanceType.SpacingPatternDistanceType,
        )
        rectangularPatterns.add(rectangularPatternInput)

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

 

0 Likes