You are absolutely right, thanks a lot too Curtis!
I'll take the opportunity to ask a question right away, it's like you know what it's about.
In the same macro/rule, which is btw an external rule that should work for all types of documents, which it does now and I'm very happy with.
Anyway, the rule checks for a certain iProperty, if it isn't there the user is shown an InputListBox where he can choose a value, at the same time the part is opened and activated so that the user can see which part it is about.
I would like to deactivate the part afterwards if it was not open before.
I don't know how to compare whether the part is already open, it is loaded I know in the background but...
And I don't know how to deactivate the part to put it back in the background.
Is this possible?
See the specific piece of code and the complete code.
Is the way in which the iProperties are allocated sufficiently stable or can it be improved?
In other words, if you see things that could be better, I'd be very happy to recommend it, thanks in advance.
See the specific piece of code and the complete code.
Try
If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
' Open document so that the user can select the Fabrication value, close if it was not opened
ThisApplication.Documents.Open(oDoc.FullFileName, True) ' Visible
ThisDoc.Document.Activate
'MessageBox.Show(bool)
CreateFabrication
'ThisDoc.Document.Activate
End If
Catch
CreateFabrication
End Try
Sub Main
''----------------------------------------------------------------------------------------------------
'' Write to textfile
'Dim strDateAndTime As String
'strDateAndTime = DateTime.Now.ToString(("yyyy-MM-dd HHmmss"))
Dim strTime As String
strTime = DateTime.Now.ToString(("HH:mm:ss"))
'Dim LogFileName As String
'LogFileName = "C:\temp\iLogic_Log_InventorPLUS " & strDateAndTime & ".txt"
'oWrite = System.IO.File.CreateText(LogFileName)
''----------------------------------------------------------------------------------------------------
'' Calculate elapsed time
'Dim oWatch As New Stopwatch
'oWatch.Start
'----------------------------------------------------------------------------------------------------
'Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument ' In case of an assembly from where components needs to be updated oDoc stays at the assy.
Dim oDoc As Inventor.Document = ThisDoc.Document ' With ThisDoc.Document oDoc is set to the actual component is
Logger.Info(strTime & " InventorPLUS: Start")
Logger.Info(strTime & " InventorPLUS: " & oDoc.FullDocumentName & vbNewLine)
' Try to run external rule, does not work, it seems that Inventor can't wait for the macro to do its thing...
'iLogicVb.RunExternalRule("C:\Workingfolder\CAD Settings\Inventor\Macros\Collapse_Browser.iLogicVb")
'InventorVb.RunMacro("ApplicationProject", "Collapse_Browser", "Collapse_Browser")
'iLogicVb.UpdateWhenDone = True
'Threading.Thread.Sleep(1000) 'in ms
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Try to remove 'old' iLogic Rules and Event Triggers
' Try
' auto = iLogicVb.Automation
' Dim iLogicAuto As Object
' iLogicAuto = auto
' Try: iLogicAuto.DeleteRule(oDoc, "PartParameters"): Catch: End Try
' Try: iLogicAuto.DeleteRule(oDoc, "SheetMetalParametersIniProperties") :Catch: End Try
' Catch
' End Try
' Dim customPropSets As PropertySets
' customPropSets = oDoc.PropertySets
' Dim customPropSet As PropertySet
' customPropSet = oDoc.PropertySets.Item("_iLogicEventsRules")
' Dim oProperty As Inventor.Property
' oProperty = cuscustomPropSetq2.Delete("PartParameters")
' oProperty = customPropSet.Delete("SheetMetalParametersIniProperties")
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
'If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kPartDocumentObject Then
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Logger.Info(strTime & " InventorPLUS: Inside PartDocument"& vbNewLine)
' Set Home
Call ThisApplication.ActiveView.GoHome
'MessageBox.Show("Wait...")
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Update iProperties
Dim oCustomPropSet As PropertySet
' Define custom property collection
oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
Try : oCustomPropSet.Item("Afmetingen").Delete : Catch : End Try
Try : oCustomPropSet.Item("FPL").Delete : Catch : End Try
Try : oCustomPropSet.Item("FPW").Delete : Catch : End Try
Try : oCustomPropSet.Item("ERP Number").Delete : Catch : End Try
Try : oCustomPropSet.Item("INK").Delete : Catch : End Try
Try : oCustomPropSet.Item("Type").Delete : Catch : End Try
Try : oCustomPropSet.Item("Remark").Delete : Catch : End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Check for Normal Part or Sheet Metal Part
If oDoc.DocumentSubType.DocumentSubTypeID = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then ' Normal Part(No Sheetmetal) Normal Part(No Sheetmetal) Normal Part(No Sheetmetal)
Logger.Info(strTime & " InventorPLUS: Normal Part(No Sheetmetal) " & vbNewLine)
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Set iProperty 'PartParameters'
Try
PartLength = Round(Measure.ExtentsLength, 1)
PartWidth = Round(Measure.ExtentsWidth, 1)
PartHeight = Round(Measure.ExtentsHeight, 1)
'PartLength = Ceil(Round(Measure.ExtentsLength, 1)/1)*1
'PartWidth = Ceil(Round(Measure.ExtentsWidth, 1)/1)*1
'PartHeight = Ceil(Round(Measure.ExtentsHeight, 1)/1)*1
iProperties.Value("Custom", "PartParameters") = PartLength & " x " & PartWidth & " x " & PartHeight
'iProperties.Value("Custom", "Mass") = Round(iProperties.Mass * 1, 1) & " kg"
Catch
End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Delete and update Sheetmetal related iProperties
Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
Try : oCustomPropSet.Item("CSINK").Delete : Catch : End Try
Try : oCustomPropSet.Item("CBORE").Delete : Catch : End Try
Try : oCustomPropSet.Item("Tapping").Delete : Catch : End Try
'Try : oCustomPropSet.Item("Thickness").Delete : Catch : End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' In a Sheetmetal part Thickness as Sheetmetal property is exported to an iPropery
' Undo this when a part is no longer a Sheetmetal part
' Attention: Normaly this iProperty can be accesed by 'oParam = oCompDef.Thickness'
Try
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition
' Get a reference to the parameter controlling the thickness.
Dim oParam As Parameter
'oParam = oCompDef.Thickness
'oParam.ExposedAsProperty = False
oParam = oCompDef.Parameters.Item(1)
If oCompDef.Parameters.Item(1).Name = "Thickness" Then oParam.ExposedAsProperty = False
Catch
End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Delete Fabrication when the value is Sheetmetal related
Try
If iProperties.Value("Custom", "Fabrication") = "B" Or iProperties.Value("Custom", "Fabrication") = "F" Then
Try : oCustomPropSet.Item("Fabrication").Delete : Catch
End Try
End If
Catch
End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Check if iPropery Fabrication exist, if not create
' In iLogic it's not possible to check if a iProperty exist, hence it's done in a function
' Creating the iPropery is done in a Sub
'If oDoc.FileSaveCounter = 0 Then
Try
If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
' Open document so that the user can select the Fabrication value, close if it was not opened
ThisApplication.Documents.Open(oDoc.FullFileName, True) ' Visible
ThisDoc.Document.Activate
'MessageBox.Show(bool)
CreateFabrication
'ThisDoc.Document.Activate
End If
Catch
CreateFabrication
End Try
'End If
End If
' -------------------------------------------------------------------------------------------------
' Check for Normal Part or Sheet Metal Part
If oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' Sheetmetal Part Sheetmetal Part Sheetmetal Part Sheetmetal Part Sheetmetal Part
Logger.Info(strTime & " InventorPLUS: Sheetmetal Part" & vbNewLine)
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' In a Sheetmetal part Thickness as Sheetmetal property is exported to an iPropery to get Thickness on a drawing
Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oDoc.ComponentDefinition
' Get a reference to the parameter controlling the thickness.
Dim oParam As Parameter
oParam = oCompDef.Thickness
oParam.ExposedAsProperty = True
oParam.CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kThreeDecimalPlacesPrecision
oParam.CustomPropertyFormat.ShowTrailingZeros = False
oParam.CustomPropertyFormat.ShowUnitsString = False
oParam.CustomPropertyFormat.Units = "mm"
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Set iProperty 'PartParameters'
Try
SheetMetalLength = Round(SheetMetal.FlatExtentsLength, 1)
SheetMetalWidth = Round(SheetMetal.FlatExtentsWidth,1)
If SheetMetalLength > SheetMetalWidth Then
iProperties.Value("Custom", "PartParameters") = SheetMetalLength & " x " & SheetMetalWidth
End If
If SheetMetalWidth > SheetMetalLength Then
iProperties.Value("Custom", "PartParameters") = SheetMetalWidth & " x " & SheetMetalLength
End If
If SheetMetalWidth = SheetMetalLength Then
iProperties.Value("Custom", "PartParameters") = SheetMetalWidth & " x " & SheetMetalLength
End If
Catch
End Try
' -------------------------------------------------------------------------------------------------
' Set iProperty 'Bend'
Dim bFlat As Boolean
Dim oSMDef As SheetMetalComponentDefinition
oSMDef = oDoc.ComponentDefinition
'Check for Flat Pattern
If oSMDef.FlatPattern Is Nothing Then
'Create Flat Pattern
Try
oSMDef.Unfold()
oSMDef.FlatPattern.ExitEdit()
oDoc.Close()
bFlat = True
Catch ' Failed to unfold part
'MessageBox.Show("Failed to create Flat Pattern")
bFlat = False
Return
End Try
Else
bFlat = True
End If
Try
If bFlat = True
Dim bends As FlatBendResults
bends = oSMDef.FlatPattern.FlatBendResults
'Count bends
Dim count As Integer = 0
For Each Bend In oSMDef.Bends
count += 1
Next
'Dim oCustomPropSet As PropertySet
'oCustomPropSet = oDoc.PropertySets("Inventor User Defined Properties")
' Needs to be checked for mirrored parts! 'Check for/create property
'Try
' intTest = oCustomPropSet.Item("Bends").Value
'Catch ex As Exception
' oCustomPropSet.Add(count, "Bends")
'End Try
'messagebox.Show("Wait...")
Try
If count = 1 Then
iProperties.Value("Custom", "Bends") = count & " Bend"
Try
If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "B+S"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "B+W"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "B+Z"
Else: iProperties.Value("Custom", "Fabrication") = "B"
End If
Catch
iProperties.Value("Custom", "Fabrication") = "B"
End Try
ElseIf count > 1 Then
iProperties.Value("Custom", "Bends") = count & " Bends"
Try
If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "B+S"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "B+W"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "B+Z"
Else: iProperties.Value("Custom", "Fabrication") = "B"
End If
Catch
iProperties.Value("Custom", "Fabrication") = "B"
End Try
Else
iProperties.Value("Custom", "Bends") = "No Bends"
Try
If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "F+S"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "F+W"
ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "F+Z"
Else: iProperties.Value("Custom", "Fabrication") = "F"
End If
Catch
iProperties.Value("Custom", "Fabrication") = "F"
End Try
End If
Catch
iProperties.Value("Custom", "Fabrication") = "F"
End Try
'Logger.Info(iProperties.Value("Custom", "Bends"))
Logger.Info(strTime & " InventorPLUS: Bends found: " & count & oDoc.FullDocumentName & vbNewLine)
End If
Catch
End Try
' -------------------------------------------------------------------------------------------------
' Check for Hole types
Dim features = oDoc.ComponentDefinition.Features
Dim HoleType As String
Dim Tapping As Boolean
Dim CSINK As Boolean
Dim CBORE As Boolean
Dim numberOfThreadedHoleFeatures As Integer = 0
For Each hole As HoleFeature In features.HoleFeatures
'MessageBox.Show (hole.Suppressed)
Select Case hole.HoleType
Case 21505
HoleType = "kDrilledHole" ' Hole is drilled (no countersink Or counterboring).
Case 21506
HoleType = "kCounterSinkHole" ' Hole is countersunk.
If hole.Suppressed = False Then CSINK = True
Case 21507
HoleType = "kCounterBoreHole" ' Hole is counterbored.
If hole.Suppressed = False Then CBORE = True
Case 21508
HoleType = "kSpotFaceHole" ' Hole is spotfaced.
Case 83912192
HoleType = "kHoleFeatureObject"
End Select
Logger.Info(strTime & " InventorPLUS: Hole type: " & HoleType & oDoc.FullDocumentName & vbNewLine)
Logger.Info(strTime & " InventorPLUS: " & ObjectTypeEnum.kHoleFeatureObject & vbNewLine)
' Tapped Hole
Dim infoTap As HoleTapInfo = hole.TapInfo
If (infoTap IsNot Nothing) Then
If hole.Suppressed = False Then numberOfThreadedHoleFeatures = numberOfThreadedHoleFeatures + 1
' Your hole changing code go's here
End If
Next
' Tapped Hole
If numberOfThreadedHoleFeatures > 0 Then
iProperties.Value("Custom", "Tapping") = "Yes"
Else
iProperties.Value("Custom", "Tapping") = "No"
End If
' Counter Sink Hole
If CSINK = True Then
iProperties.Value("Custom", "CSINK") = "Yes"
Else
iProperties.Value("Custom", "CSINK") = "No"
End If
' Counter Bore Hole
If CBORE = True Then
iProperties.Value("Custom", "CBORE") = "Yes"
Else
iProperties.Value("Custom", "CBORE") = "No"
End If
'MsgBox("Number of holes with thread: " & numberOfThreadedHoleFeatures)
'Logger.Info("Number of holes with thread: " & numberOfThreadedHoleFeatures)
' you can skip this loop if you just want to count the number of tread features
'For Each thread As ThreadFeature In features.ThreadFeatures
' Your thread changing code go's here
'Next
'MsgBox("Number of threadfeatures (on a extrude or cut feature): " _
' & features.ThreadFeatures.Count)
' -------------------------------------------------------------------------------------------------
oDoc = Nothing
End If
'ElseIf ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Logger.Info(strTime & " InventorPLUS: Inside AssemblyDocument"& vbNewLine)
' Set Home view does not work, it looks like Inventor can't wait for the Home position to be taken...
'Call ThisApplication.ActiveView.GoHome
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Import latest BOM Customization settings
Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oDoc.ComponentDefinition
Dim oBOM1 As BOM
oBOM1 = oAsmDef.BOM
Dim filename As String ' Assume the file exists
filename = "C:\Workingfolder\CAD Settings\Inventor\Personal Settings\HML\BOM_Customization_Settings.xml"
Call oBOM1.ImportBOMCustomization(filename)
Beep
' Update Mass properties
ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Check Assembly Type and set iProperty for Weldment
If oDoc.DocumentSubType.DocumentSubTypeID = "{28EC8354-9024-440F-A8A2-0E0E55D635B0}" Then ' Stands for Weldassy
Logger.Info("WeldSheetmetal Part")
Try: iProperties.Value("Custom", "Fabrication") = "W": Catch: End Try
Else
' Do not make empty! In case it's not an Inventor Weldment but still a Weld Assembly
' Until we agree that all Weld Assemblies needs to be Inventor Weldments
'Try: iProperties.Value("Custom", "Fabrication") = "": Catch: End Try
End If
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Check if iPropery Fabrication exist, if not create, only ask for new Assemblies which are not saved yet...
' In iLogic it's not possible to check if a iProperty exist, hence it's done in a function
' Creating the iPropery is done in a Sub
If oDoc.FileSaveCounter = 0 Then
Try
If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
CreateFabrication
End If
Catch
CreateFabrication
End Try
End If
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Set iProperty PartParameters
Try
PartLength = Round(Measure.ExtentsLength, 1)
PartWidth = Round(Measure.ExtentsWidth, 1)
PartHeight = Round(Measure.ExtentsHeight, 1)
'PartLength = Ceil(Round(Measure.ExtentsLength, 1)/1)*1
'PartWidth = Ceil(Round(Measure.ExtentsWidth, 1)/1)*1
'PartHeight = Ceil(Round(Measure.ExtentsHeight, 1)/1)*1
iProperties.Value("Custom", "PartParameters") = PartLength & " x " & PartWidth & " x " & PartHeight
'iProperties.Value("Custom", "Mass") = Round(iProperties.Mass * 1, 1) & " kg"
Catch
End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Update iProperties
Dim oCustomPropSet As PropertySet
' Define custom property collection
oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
'MessageBox.Show("Assy...")
Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
Try : oCustomPropSet.Item("Threaded holes").Delete : Catch : End Try
Try : oCustomPropSet.Item("Afmetingen").Delete : Catch : End Try
Try : oCustomPropSet.Item("INK").Delete : Catch : End Try
Try : oCustomPropSet.Item("ERP Number").Delete : Catch : End Try
Try : oCustomPropSet.Item("Type").Delete : Catch : End Try
Try : oCustomPropSet.Item("Remark").Delete : Catch : End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
'ElseIf ThisApplication.ActiveDocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Logger.Info(strTime & " InventorPLUS: Inside DrawingDocument"& vbNewLine)
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Update Copied Model iProperties
' Re-copy chosen iProperties from source model to the drawing
Try
Dim oControlDef As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("UpdateCopiedModeliPropertiesCmd")
oControlDef.Execute2(True)
Catch
End Try
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Zoom all
ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomallCmd").Execute
iLogicVb.UpdateWhenDone = True
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
' Update drawing to have up to date mass
ThisApplication.ActiveDocument.update()
' -------------------------------------------------------------------------------------------------------------------------------------------------------------
End If
Logger.Info(strTime & " InventorPLUS: End " & vbNewLine)
''----------------------------------------------------------------------------------------------------
''Calculate and report elapsed time.
'oWatch.Stop()
'oWrite.WriteLine(vbNewLine & "End of log" & vbNewLine & ("Elapsed Time: " & oWatch.ElapsedMilliseconds.ToString & " Milliseconds"))
''----------------------------------------------------------------------------------------------------
'' Close log file, optional open the log file
'oWrite.Close()
'ThisDoc.Launch(LogFileName)
'----------------------------------------------------------------------------------------------------
End Sub
Function CheckIfPropertyExist(strProp As String) As Boolean
' Check if Fabrication exist
Try
Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument
Dim oCustomPropSet As PropertySet
' Define custom property collection
oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
' Try to see if a value is there for Fabrication
Prop = oCustomPropSet.Item(strProp).Value
CheckIfPropertyExist = True
Catch
CheckIfPropertyExist = False
End Try
End Function
Sub CreateFabrication
Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument
Dim oCustomPropSet As PropertySet
' Define custom property collection
oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Try: oCustomPropSet.Add("", "Fabrication"): Catch: End Try ' Add propery with empty value: catch
Dim oList As New ArrayList
oList.Add("S = Machining")
oList.Add("W = Weldment")
oList.Add("Z = Sawing")
'oList.Add(" Empty")
AskForValue:
InputValue = Left (InputListBox("Please select a value", oList, InputValue ,Title := "InventorPLUS", ListName := "Fabrication"),1)
'InputValue = InputBox("Please enter a value for Fabrication", "InventorPLUS", "")
If InputValue = "" Then
'GoTo AskForValue
Else
iProperties.Value("Custom", "Fabrication") = InputValue
End If
End Sub