Copy component originating within a referenced component?

Copy component originating within a referenced component?

Anonymous
Not applicable
2,243 Views
4 Replies
Message 1 of 5

Copy component originating within a referenced component?

Anonymous
Not applicable

I'm trying to use Occurrences.addExistingComponent to copy a subcomponent of a referenced component into a (non-referenced) subcomponent of the current design. However, it appears that this operation is unsupported. What I'm doing appears to work fine when no referenced components are involved, or if the copy source is itself a referenced component; but when the copy source is a subcomponent of a referenced component, I get a "RuntimeError: 3 : add operation failed".

 

Trying to achieve the same thing by copying and pasting from the UI doesn't appear to work either, so this may not strictly be an API issue.

(Relatedly, I'm finding that BRepBody.copyToComponent fails to copy bodies from within referenced components. This doesn't work in the UI either, as described in https://forums.autodesk.com/t5/fusion-360-design-validate/copy-paste-body-from-linked-component/m-p/...)

Is there some sort of API call I'm missing that I can use to "localize" an external subcomponent to the current design? Or any other method/workaround for creating a local copy of an external subcomponent?


Tack-on tanget: I wanted to provide a script that demonstrated this issue, but I couldn't find API calls that would let me set up a referenced component between two Documents. If someone could point me in the right direction there, I'd be happy to add a demo script.

0 Likes
Accepted solutions (1)
2,244 Views
4 Replies
Replies (4)
Message 2 of 5

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

You can create a local copy of referenced component by Occurrence.breakLink() API and then copy the sub component of copied component to sub component of current design. If you want to keep referenced component unchanged, you can create a new local occurrence of the referenced component and do copy operation and then delete the new local occurrence after copy.

 

The codes as below demo how to create a new local occurrence of referenced component. I am assuming there is only one referenced document in current design. 

import adsk.core, adsk.fusion, traceback

def run(context):
  ui = None
  try:
    app = adsk.core.Application.get()
    ui  = app.userInterface
    des = adsk.fusion.Design.cast(app.activeProduct)
    root = des.rootComponent
    
    sourceFile = app.activeDocument.documentReferences.item(0).dataFile  
    root.occurrences.addByInsert(sourceFile, adsk.core.Matrix3D.create(), False)

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

BTW: we have a new api to get document reference from an occurrence. Unfortunately it wasn't implemented yet. It would be available in future versions. 

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
Message 3 of 5

Anonymous
Not applicable

Thanks; I'm successfully using localOcc = dst.occurrences.addByInsert(src.component.parentDesign.parentDocument.dataFile, foo, False) to create a temporary local copy of the referenced component.

 

However, because this copies the whole referenced component and not just the subcomponent I'm interested in (src.component), I now need to locate the component equivalent to src.component that's within localOcc.

 

Doing some kind of recursive iteration though localOcc.childOccurrences and looking for something with a matching name seems inefficient and error-prone (as I understand it, the local equivalent I'm searching for may have been automatically renamed if the current document already contained a component of the same name). I suppose calculating a src.component.parentDesign.rootComponent.occurrences[x].childOccurrences[y].childOccurrences[z].… path back to src, and substituting into localOcc.childOccurrences[x].childOccurrences[y].… seems like it might work, if the ordering is consistent between the referenced component and the local copy, but I'm not sure if that's necessarily the case (also, this method seems inefficient, painfully so if src was provided with NativeObjects such that src.assemblyContext.assemblyContext.… does not trace back up to its root).

 

Will the ordering of occurrences/childOccurrences be consistent between a referenced component and a local copy of that component? Or, better yet, am I missing an efficient solution here to finding a subcomponent of localOcc that is equivalent to src.component?

0 Likes
Message 4 of 5

marshaltu
Autodesk
Autodesk

Hello,

 

Unfortunately I didn't find good solution for that. We had an API to get full path name of occurrence. However as you said, the name of occurrence would be changed(index will increment) if it is duplicated to existing one. Theoretically the hierarchy of new copy should be same with the original one if the original one is not out of date. So you have to position the source occurrence in the hierarchy of original sub component and find the new occurrence by same position in the hierarchy of new copy.

 

I will create an enhancement task in our backlog to improve that.

 

Thanks,

Marshal 



Marshal Tu
Fusion Developer
>
0 Likes
Message 5 of 5

Anonymous
Not applicable

Thanks for the help. In my particular case, I think I can modify my script to have the relevant occurrence-index-path available, so that should work for me.

 

A future enhancement to make copying subcomponents/geometry from other documents more direct would be appreciated 🙂

0 Likes