Hi,
I'm trying to figure out how to detect from external rule if one parametr exist in Assembly/or part. If not, then create it and continue. If yes, skip the creation and continue in rule.
So far without the parameter detection, I got this:
FILE_NAME = "\PDF_SAVE\" 'Path setting
oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParameter=oMyParameter.AddByValue("EXTERNAL", "True", UnitsTypeEnum.kTextUnits) 'Create EXTERNAL parameter
oParameter=oMyParameter.AddByValue("PDF", "True", UnitsTypeEnum.kTextUnits) ' Create PDF parameter 'So far this works, but it creates EXTERNAL_1 if EXTERNAL exists
'Check if Save As Should proceed If Parameter("EXTERNAL") = "True" Then FILE_SAVEAS = ThisDoc.Path & FILE_NAME & ThisDoc.FileName(False) Else FILE_SAVEAS = ThisDoc.Path & ThisDoc.FileName(False) End If If Parameter("EXTERNAL") = "False" Then Return 'No Save As set? Then close the rule 'PDF Yes/No? If Parameter("PDF") = "True" Then ThisDoc.Document.SaveAs(FILE_SAVEAS & (".pdf") , True) End If
Any Advice for this parameter detect for first block of code? Thank you
Solved! Go to Solution.
Solved by Ezekiel12. Go to Solution.
Solved by MechMachineMan. Go to Solution.
Thx! That helped.
Result:
oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters Try 'Change value of param Parameter("EXTERNAL") = "test" Catch 'Create Param as it doesn't exist oParameter=oMyParameter.AddByValue("EXTERNAL", "True", UnitsTypeEnum.kTextUnits) End Try
I know its been several years, but this is such a highly searched issue, I just wanted to offer another alternative statement for the Try portion for similar rules in the future.
To avoid actually changing the value of the parameter in question, I simply try to assign the value of the parameter to a "dummy variable".
If it can't assign the value, because it can't find the parameter, it will fail, which leads to the Catch portion.
If it succeeds, it just assigned the parameters value to a useless dummy variable that you don't intend to use.
Example:
oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
Try
'Try to assign the value of the parameter to a useless dummy variable.
oDummyVariable = Parameter("External")
Catch
'It failed, so create the parameter.
oParameter = oMyParameter.AddByValue("External" , "True", UnitsTypeEnum.kTextUnits)
End Try
Wesley Crihfield
(Not an Autodesk Employee)
While effective at quick coding, it also makes for very slow processing.
If statements and loops process faster than a try/catch block handling an error. Using Try Catch method to populate an unknown property is really 'sloppy' code, and should not be published as the proper way to perform a task. Most objects can be found using find methods, or loops, or even if contains statements. If all else fails use reflection to probe for properties of an unknown object (same thing intellisense uses).
Then after it is or is not found, you continue. Sure you write more code, but your code will out perform the try/catch ridden code every time. If you find yourself looping through hundreds of instances of this try/catch method to access properties, you and your users will feel the lag.
I didn't know a single Try statement took so much longer to process than a "Loop" statement with accompanying "If Then" statements. Doesn't sound logical, but I'll take your word for it. Good to know.
Then I suppose the iLogic code to check for a specific Parameter might look something like the following:
Dim oDoc As Document oDoc = ThisDoc.Document Dim oPdoc As PartDocument oPdoc = ThisDoc.Document Dim oPrtCompDef As PartComponentDefinition Dim oAdoc As AssemblyDocument oAdoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition Dim oParams As Parameters Dim oParam As Parameter If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then oPrtCompDef = oPdoc.ComponentDefinition oParams = oPrtCompDef.Parameters ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then oAsmCompDef = oAdoc.ComponentDefinition oParams = oAsmCompDef.Parameters Else MessageBox.Show("This rule [ " & iLogicVb.RuleName & " ] only works on either Part Documents or Assembly Documents.", "WRONG DOCUMENT TYPE") Return End If For Each oParam In oParams If oParam.Param.Name = "EXTERNAL" Then If oParam.Value = "True" Then 'Your statement Else 'Your statement End If ElseIf oParam.Param.Name = "PDF" Then If oParam.Value = "True" Then 'Your statement Else 'Your statement End If End If Next
Wesley Crihfield
(Not an Autodesk Employee)
I use a separate function that returns the result, that way I don't have to recode:
... running method (such as Sub Main)
Dim paraHeight As Parameter = FindParameter(InvDoc, "G_H")
... end of running method
Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter
Dim cd As ComponentDefinition = Nothing
Select Case invdoc.DocumentType
Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
Dim docAssembly As Inventor.AssemblyDocument = invdoc
cd = docAssembly.ComponentDefinition
Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
Dim docPart As Inventor.PartDocument = invdoc
cd = docPart.ComponentDefinition
End Select
If cd IsNot Nothing Then
For Each param As Parameter In cd.Parameters
If param.Name = strParameter Then
Return param
End If
Next
End If
Return Nothing
End Function
I'm a newbie at iLogic. I need exactly what the op is asking for. The short public function code seems nice to me but I can't get it to work. I don't know much about subs and public functions. What do the first 3 lines stand for and how do I make this code work or what would the issue be?
I did this for the first 3 lines:
Sub Main () Dim paraHeight As Parameter = FindParameter(InvDoc, "G_H") End Sub
Many thanks!
Hello
These 3 lines only call the function FindParameter with the two expected Arguments Inventor.Document and ParameterName. You also need the posted function. The function returns either the parameter you're searching or, in case this parameter doesn't exist, returns Nothing. If Nothing, you can create this parameter, otherwise you can get it's value or change it.
Here is an example that uses two functions, one to check the parameter exists same as posted above and one to get the user parameter collection then if the user parameter doesn't exist it can be added.
Sub Main() Dim invdoc As Document invdoc = ThisDoc.Document 'Function returns UserParameters Collection oUserParams = GetUserParams(invdoc) '[paraHeight 'Function returns object parameter paraHeight = FindParameter(invdoc, "paraHeight") If paraHeight IsNot Nothing Then MessageBox.Show("Parameter does exist", "Title") 'Do something if found Else MessageBox.Show("Parameter does not exist", "Title") paraHeight = oUserParams.AddByValue("paraHeight", 1, UnitsTypeEnum.kCentimeterLengthUnits) End If '] '[paraHeight1 'Function returns object parameter paraHeight1 = FindParameter(invdoc, "paraHeight1") 'Check if object is Nothing If paraHeight1 IsNot Nothing Then MessageBox.Show("Parameter does exist", "Title") 'Do something if found Else MessageBox.Show("Parameter does not exist!", "Title") paraHeight1 = oUserParams.AddByValue("paraHeight1",1, UnitsTypeEnum.kCentimeterLengthUnits) End If '] End Sub Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter Dim cd As ComponentDefinition = Nothing Select Case invdoc.DocumentType Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject MessageBox.Show("Is a Assembly", "Title") Dim docAssembly As Inventor.AssemblyDocument = invdoc cd = docAssembly.ComponentDefinition Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject MessageBox.Show("Is a Part", "Title") Dim docPart As Inventor.PartDocument = invdoc cd = docPart.ComponentDefinition End Select If cd IsNot Nothing Then For Each param As Inventor.Parameter In cd.Parameters If param.Name = strParameter Then Return param End If Next End If Return Nothing End Function Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters Dim cd As ComponentDefinition = Nothing Select Case invdoc.DocumentType Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject MessageBox.Show("Is a Assembly", "Title") Dim docAssembly As Inventor.AssemblyDocument = invdoc cd = docAssembly.ComponentDefinition Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject MessageBox.Show("Is a Part", "Title") Dim docPart As Inventor.PartDocument = invdoc cd = docPart.ComponentDefinition End Select If cd IsNot Nothing Then Dim oParams As Parameters oParams = cd.Parameters Dim oUserParams As UserParameters oUserParams = oParams.UserParameters Return oUserParams End If Return Nothing End Function
Thank you, it works perfectly!
How would I change it for an iProperty, instead of a Parameter. I have here the "Shape" Parameter but I'm interested in defining it as an iProperty (so that I can view / modify it from a BOM).
I tried some things on my own, but I'm probably not defining things properly and I can't get it to work.
(And I don't know, maybe I should have started a new thread...)
Thanks again!
Sub Main() Dim invdoc As Document invdoc = ThisDoc.Document 'Function returns UserParameters Collection oUserParams = GetUserParams(invdoc) 'Parameter Shape 'Function returns object parameter paraShape = FindParameter(invdoc, "Shape") If paraShape IsNot Nothing Then MessageBox.Show("Parameter 'Shape' already exists", "Title") 'Do something if found Else MessageBox.Show("Parameter 'Shape' does not exist. It will be created with an 'xx' value, by default", "Shape Parameter") paraShape = oUserParams.AddByValue("Shape", "xx", kTextUnits) End If End Sub Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter Dim cd As ComponentDefinition = Nothing Select Case invdoc.DocumentType Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject 'MessageBox.Show("Is a Assembly", "Title") Dim docAssembly As Inventor.AssemblyDocument = invdoc cd = docAssembly.ComponentDefinition Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject 'MessageBox.Show("Is a Part", "Title") Dim docPart As Inventor.PartDocument = invdoc cd = docPart.ComponentDefinition End Select If cd IsNot Nothing Then For Each param As Inventor.Parameter In cd.Parameters If param.Name = strParameter Then Return param End If Next End If Return Nothing End Function Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters Dim cd As ComponentDefinition = Nothing Select Case invdoc.DocumentType Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject 'MessageBox.Show("This is an Assembly", "Title") Dim docAssembly As Inventor.AssemblyDocument = invdoc cd = docAssembly.ComponentDefinition Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject 'MessageBox.Show("This is a Part", "Title") Dim docPart As Inventor.PartDocument = invdoc cd = docPart.ComponentDefinition End Select If cd IsNot Nothing Then Dim oParams As Parameters oParams = cd.Parameters Dim oUserParams As UserParameters oUserParams = oParams.UserParameters Return oUserParams End If Return Nothing End Function
That would be a new thread for sure.
Can't find what you're looking for? Ask the community or share your knowledge.