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 ']
Solved! Go to Solution.
Solved by A.Acheson. Go to Solution.
You can replace component with occurrence ("oOcc") since it is the same thing any how.
ComponentOccurrence.Replace help pageComponentOccurrence.Replace( FileName As String, ReplaceAll As Boolean )
Can't find what you're looking for? Ask the community or share your knowledge.