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: 

iLogic detect if parameter exist - if not, create it

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
Ezekiel12
8781 Views, 12 Replies

iLogic detect if parameter exist - if not, create it

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

12 REPLIES 12
Message 2 of 13
MechMachineMan
in reply to: Ezekiel12

Try
'Change value of param
Catch
'Create Param as it doesn't exist
End Try

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 13
Ezekiel12
in reply to: MechMachineMan

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 
Message 4 of 13
MechMachineMan
in reply to: Ezekiel12

Warning tho: if the try doesn't error out, it will actually change the value of the parameter! So you should actually do something like:

Try
Parameter("EXTERNAL") = Parameter("EXTERNAL")
.......

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 5 of 13
WCrihfield
in reply to: MechMachineMan

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

EESignature

(Not an Autodesk Employee)

Message 6 of 13
JamieVJohnson2
in reply to: WCrihfield

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.

jvj
Message 7 of 13
WCrihfield
in reply to: JamieVJohnson2

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

EESignature

(Not an Autodesk Employee)

Message 8 of 13
JamieVJohnson2
in reply to: WCrihfield

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

jvj
Message 9 of 13
emanuel.c
in reply to: JamieVJohnson2

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!

Message 10 of 13
Ralf_Krieg
in reply to: Ezekiel12

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.


R. Krieg
RKW Solutions GmbH
www.rkw-solutions.com
Message 11 of 13
A.Acheson
in reply to: Ralf_Krieg

@emanuel.c 

 

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
	

 

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

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

 

Message 13 of 13
A.Acheson
in reply to: emanuel.c

@emanuel.c

That would be a new thread for sure. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report