Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't use "Component" commands if inside a drawing, even if its not being run?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
C_Haines_ENG
134 Views, 2 Replies

Can't use "Component" commands if inside a drawing, even if its not being run?

Hey all,

 

Im trying to make a two in one script, one that will replace the model being referenced in a draiwng and and one that will replace all components in an assembly drawing. However upon running this rule in a drawing file it throws an error and says "Component: This function can only be used in an assembly.".  While I understand that ive hidden the component command to only run if its in an assembly drawing, so this command isnt even being run, just having it in the script seems to trigger this error.

 

Does anyone know a workaround to this and why iLogic is being incredibly stupid about this? Ive put my entire code below:

'[ REPLACE DRAWING COMPONENTS ]

'CHECK IF ACTIVE FILE IS DRAWING OR ASSEMBLY
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	
	'DEFINE MODEL DOCUMENT TO THE CURRENT VIEW INSERTED'
	oDoc = ThisDrawing.Document
	oView = ActiveSheet.DrawingViews.NativeEntity.Item(1)
	Dim oFD As FileDescriptor
	Dim oModelDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
	
	
	If IsNothing(oModelDoc) Then
		MsgBox("A Model Must Be Inserted To Use This Tool.", vbCritical, "No Model Found!")
		Return
	End If
	
	'CALL ON INVENTORS "REFERERENCE MODEL" API
	oFD = oDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
	
	'REPLACE REFERENCED MODEL WITH PART THAT HAS SAME NAME AS DRAWING
	Try
		oFD.ReplaceReference(ThisDoc.Path & "\" & ThisDoc.FileName & ".iam")
	Catch
		oFD.ReplaceReference(ThisDoc.Path & "\" & ThisDoc.FileName & ".ipt")
	End Try
	oDoc.Update
	
	'LOCATE FILE'
	oModelDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	modelName = IO.Path.GetFileName(oModelDoc.FullFileName)
	
	'CHANGE IPROPERTY OF MODEL AND REFRESH PAGE'
	iProperties.Value(modelName, "Project", "Part Number") = ThisDoc.FileName(False)
	ThisApplication.ActiveDocument.Update
	
	iProperties.Value("Project", "Creation Date") = Date.Today
	
	oDoc.Update
	']
	
Else
	
	'[ REPLACE ASSEMBLY COMPONENTS ]
	'top-level assembly document
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument 
	Dim oAsmDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
	
	'Iterate through all of the occurrences at the top level
	For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences
	
		'get name of this occurrence
		Dim oName As String = oOcc.Name 
		
			Replacement = ThisDoc.Path & "\" & Left(oOcc.Name,InStr(oOcc.Name, ":") - 1) & ".ipt"
			Try
				Component.Replace(oName, Replacement, True) 
			Catch
				MessageBox.Show("Missing Components in Local Folder!", "ERROR")
			End Try

Next
	
End If

']

 

2 REPLIES 2
Message 2 of 3
A.Acheson
in reply to: C_Haines_ENG

Hi @C_Haines_ENG 

You can replace component with occurrence ("oOcc")  since it is the same thing any how. 

ComponentOccurrence.Replace help page

ComponentOccurrence.ReplaceFileName As String, ReplaceAll As Boolean )

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 3
C_Haines_ENG
in reply to: A.Acheson

Thanks, should have thought of that.

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

Post to forums  

Autodesk Design & Make Report