Replace derived part via API is limited vs the right click menu

Replace derived part via API is limited vs the right click menu

pball
Mentor Mentor
454 Views
6 Replies
Message 1 of 7

Replace derived part via API is limited vs the right click menu

pball
Mentor
Mentor

I noticed today that the right click option for Replace Base Component on a derived part does not appear to care if the original and replacement part have the same document ID. Where as with the API example below the replacement part needs to share the document ID of the original derived part.

 

I'd like to update my addin to work the same way as the right click menu does, since it lets you replace a grease fitting with a pulley. Does anyone have a clue how the right click feature works and if it can be emulated with the API? Otherwise I'll probably just remove my script and point everyone to the right click menu.

 

pball_0-1723649565206.png

 

Try
    Dim oPartDoc As Document = ThisDoc.Document
    Dim oRefPartDoc As DocumentDescriptor= oPartDoc.ReferencedDocumentDescriptors.Item(1)

    oRefPartDoc.ReferencedFileDescriptor.ReplaceReference("FILE NAME HERE")
    oPartDoc.Update()

Catch ex As Exception
    MsgBox("Model seems to be not the same!")

End Try

 

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
455 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @pball.  That is not something I do a lot of manually, so even less often by code, but it looks like you may be approaching this from the wrong angle, so to speak.  I believe you need to be using the DerivedPartComponent.Replace method to do that.

Below is a quickie, untested code sample, to get you there.

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oDPC As DerivedPartComponent = oPDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Item(1)
Dim sReplacementFullFileName As String = "C:\Temp\MyOtherPart.ipt"
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oOptions.Add("ModelState", "AlteredState1")
oDPC.Replace(sReplacementFullFileName, oOptions)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

DonStauffer99
Advocate
Advocate

That seems to suffer from exactly the same problem. Something like an invalid parameter error. This is a devastating limitation to the API, and it seems like there's no reason for it, since you can do it in the browser. You just can't automate it.

0 Likes
Message 4 of 7

mateusz_baczewski
Advocate
Advocate

@pball @DonStauffer99 

Hi,

I managed to replace the base component using this:

 

Sub Main 

	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim oRefComponents As ReferenceComponents = oPartCompDef.ReferenceComponents
	Dim oDerivedPartComponents As DerivedPartComponents = oRefComponents.DerivedPartComponents
	Dim oDerivedPartComponent As DerivedPartComponent = oDerivedPartComponents.Item(1)
	
	Dim oTo As TransientObjects = ThisApplication.TransientObjects
	Dim oNameValue As NameValueMap = oTo.CreateNameValueMap
	
	oNameValue.Add("ModelState", "MODEL/STATE/NAME")
	
	
	oDerivedPartComponent.Replace("PATH/TO/YOUR/PART",oNameValue)

End Sub
If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

0 Likes
Message 5 of 7

DonStauffer99
Advocate
Advocate

I'll try it. Thanks!

0 Likes
Message 6 of 7

DonStauffer99
Advocate
Advocate

It worked! ThankYouThankYouThankYouThankYouThankYou!

Message 7 of 7

DonStauffer99
Advocate
Advocate

Interestingly, this appears to be undocumented. If it's been deprecated, I wonder what the replacement is?

0 Likes