Curious if anyone can help debug this script I wrote which is intended to be ran from a part or drawing where the script determines if it is being ran in a part or drawing and if in a part it deletes certain parameters from the User Parameters list, the adds sheet metal values to the custom iProperties list. It runs correctly on parts however when run from the drawing it deletes the parameters but doesn't add the iProperties to the part
I'd like to keep the single function although I think if it was written to have 2 functions then it would probably work
Sub Main()
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim smLen, smWid As String
Dim refParams As UserParameter
If oDoc.DocumentType = kassemblydocumentobject Then
MsgBox("Only run on parts or drawings")
Exit Sub
End If
If oDoc.DocumentType = kPartDocumentObject Then
Dim param As Parameter
Try :
param = oDoc.ComponentDefinition.Parameters.Item("Length")
param.Delete
Catch :
End Try
Try :
param = oDoc.ComponentDefinition.Parameters.Item("Width")
param.Delete
Catch :
End Try
Call AddParams
End If
If oDoc.DocumentType = kdrawingdocumentobject Then
Dim oRef As Document
Dim refDocs As Integer
For Each oRef In oDoc.ReferencedDocuments
refDocs = oDoc.ReferencedDocuments.Count
Try:
If refDocs < 2 Then
If oRef.PropertySets.Item("Design Tracking Properties").Item("Document SubType Name").Value <> "Sheet Metal" Then
MsgBox("Part must be sheet metal to apply sheet metal functions")
Exit Sub
Else
Parameter.Quiet = True
Try :
Dim lenParam As UserParameter = oRef.ComponentDefinition.Parameters.UserParameters.item("Length")
Dim widParam As UserParameter = oRef.ComponentDefinition.Parameters.UserParameters.item("Width")
widParam.Delete
lenParam.Delete
oRef.Activate
Call AddParams
oDoc.Activate
Catch :
oRef.Activate
Call AddParams
oDoc.Activate
'MsgBox("error deleting parameters?")
End Try
End If
End If
Catch :
oRef.Activate
Call AddParams
oDoc.Activate
End Try
Next
oDoc.Update2
End If
End Sub
Function AddParams
Dim oPart As Document = ThisApplication.ActiveDocument
'MsgBox(oPart.FullFileName) ' Part is active part?!
iProperties.Value("Custom", "Length") = "=<Sheet Metal Length>"
iProperties.Value("Custom", "Width") = "=<Sheet Metal Width>"
iProperties.Value("Custom", "Visual Description") = "=<Sheet Metal Rule>"
End Function
Hi @e_frissell. The portion of your code that deals with a drawing document seems rather long and complicated to follow, with one Try...Catch statement nested within another, and both having a lot of stuff going on within them. But I did attempt to edit it anyways, to see if I could fix what was wrong. The main thing I changed was how the document types were being checked, and how the Sub routine works. I changed that other routine to accept a Document as input, to make it more accurate / efficient, and switch it from using the simple iLogic snippets to Inventor API code for handling the custom iProperties, that way we know which document is being edited there.
Not super sure what the purpose of the outer Try...Catch statement was, since the functionality of finding and deleting parameters was enclosed in the inner Try...Catch statement, which seems like the only area that might fail. Also not sure why you seem to want to activate the referenced model document before editing the custom iProperties, then activate the drawing again afterwards. I do not think the model needs to be activated to make those changes (just open in the background).
Sub Main
Dim oDoc As Document = ThisDoc.Document
If TypeOf oDoc Is AssemblyDocument Then
MsgBox("Only run on parts or drawings")
Exit Sub
End If
If TypeOf oDoc Is PartDocument Then
Dim oPDoc As PartDocument = oDoc
Dim oParams As Inventor.Parameters = oPDoc.ComponentDefinition.Parameters
Dim oParam As Inventor.Parameter
Try
oParam = oParams.Item("Length")
oParam.Delete
Catch
End Try
Try
oParam = oParams.Item("Width")
oParam.Delete
Catch
End Try
AddParams(oPDoc)
End If
If TypeOf oDoc Is DrawingDocument Then
Dim oRefDocs As DocumentsEnumerator = oDoc.ReferencedDocuments
Dim iRefDocsCount As Integer = oRefDocs.Count
If iRefDocsCount = 0 Then Return '<<< assuming you are only trying to edit model files, not the drawing >>>
Dim oRefDoc As Document
For Each oRefDoc In oRefDocs
Try
If iRefDocsCount = 1 Then
If oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Document SubType Name").Value <> "Sheet Metal" Then
MsgBox("Part must be sheet metal to apply sheet metal functions")
Exit Sub
Else 'referenced document is a sheet metal part
Dim oSMPart As PartDocument = oRefDoc
Dim oUParams As UserParameters = oSMPart.ComponentDefinition.Parameters.UserParameters
Try
Dim lenParam As UserParameter = oUParams.Item("Length")
Dim widParam As UserParameter = oUParams.Item("Width")
widParam.Delete
lenParam.Delete
oSMPart.Activate
AddParams(oSMPart)
oDoc.Activate
Catch
oSMPart.Activate
AddParams(oSMPart)
oDoc.Activate
'MsgBox("error deleting parameters?")
End Try
End If 'end of sheet metal check
End If 'end of referenced documents count check <<< !!! no responce if more than one !!! >>>
Catch
oRefDoc.Activate
AddParams(oRefDoc)
oDoc.Activate
End Try
Next 'oRefDoc
oDoc.Update2
End If 'end of document type check
End Sub
Sub AddParams(oDoc As Inventor.Document)
'MsgBox(oPart.FullFileName) ' Part is active part?!
Dim oCProps As Inventor.PropertySet = oDoc.PropertySets.Item(4)
Dim oCProp As Inventor.Property
Try
oCProp = oCProps.Item("Length")
Catch
oCProp = oCProps.Add("=<Sheet Metal Length>", "Length")
End Try
oCProp = Nothing
Try
oCProp = oCProps.Item("Width")
Catch
oCProp = oCProps.Add("=<Sheet Metal Width>", "Length")
End Try
oCProp = Nothing
Try
oCProp = oCProps.Item("Width")
Catch
oCProp = oCProps.Add("=<Sheet Metal Rule>", "Visual Description")
End Try
End Sub
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
(Not an Autodesk Employee)
Thanks! I always appreciate your responses
I have a bunch of drawing utilities in my drawing template that lets me edit all my part/assembly iProperties from the drawing which tends to be way faster - occasionally some people have put multiple parts/assemblies in a single drawing and I used the first try/catch statement to try to find the right part to edit. The script started off as one of those iLogic rules and in this case I think the first try/catch statement became redundant as the portion where it finds the right part is irrelevant since it should only be ran on parts. This probably also explains why there's a for each loop within the code when it runs on a drawing that should only contain 1 part file
Regarding the active doc - that was done because I couldn't think of a good way to call the function and edit the document I wanted. In the case of running on a part document it was really easy to code something that worked there and when the function was called it found the part as the active document. When ran on a drawing I hoped that if the part file was activated then as the active document the script would behave similarly to if it was ran on a part document - thus the original function would still activate. Initially it had the screen updating set to false and after the script ran Inventor became inoperable so got rid of the screen updating and left it. That was around the point where I realized I needed some help haha
The refdocs = 0 was a nice add. I used the original "If refDocs < 2 then...." as some other script had a problem with refDocs = 1 and wouldn't edit the part properties and that just became the default way of doing things. There could be other reasons it did what it did and I'm not knowledgeable enough to know 'why', but that was what fixed it and I kinda rolled with it since.
Thanks a bunch for the help once again, I've learned a ton from your posts
😂😋 I know what you mean. My stuff wasn't always neat or efficient either, and some of the less used / less important stuff still isn't. Most of it has gone through several waves of updates, revisions, and coding style changes over the years as I have learned and changed my coding style.
Wesley Crihfield
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.