Link a parameters between an assembly and a drawing with ilogic

Link a parameters between an assembly and a drawing with ilogic

jgomis.ext
Enthusiast Enthusiast
1,094 Views
12 Replies
Message 1 of 13

Link a parameters between an assembly and a drawing with ilogic

jgomis.ext
Enthusiast
Enthusiast

Hi,

I would like to link a parameters that I have in an assembly with a parameters of a drawing, but I don't know how to do this. Someone know somethings about it ?

This by advance for any help 🙂

0 Likes
Accepted solutions (1)
1,095 Views
12 Replies
Replies (12)
Message 2 of 13

Cadkunde.nl
Collaborator
Collaborator

Well I wrote something very quickly

 

Dim strLinkedParam As String = "Test"

Dim drawdoc As DrawingDocument = ThisDoc.Document
Dim doc As Inventor.Document

'stop if there is no model document
Try
	doc = ThisDrawing.ModelDocument
Catch
	Exit Sub
End Try

'you want an assembly, so exit script if not
If Not doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	Exit Sub
End If

Dim modeldoc As Inventor.AssemblyDocument = drawdoc.ReferencedDocuments(1)

Dim modelparams As Inventor.Parameters = modeldoc.ComponentDefinition.Parameters
Dim drawingparams As Inventor.Parameters = drawdoc.Parameters

'Important, both parameters need to exist or this crashes
'CASE SENSITIVE
drawingparams.Item(strLinkedParam).Value = modelparams.Item(strLinkedParam).Value

 But it does not cover all scenario's.

If modeldocument is a part, or if the parameter needs to be created on the drawing etc

0 Likes
Message 3 of 13

WCrihfield
Mentor
Mentor

Hi @jgomis.ext.  I do not believe you can truly 'link' a parameter directly between an assembly and a drawing.  You can 'Link' parameters between model files, so that one will 'drive' the other, but a drawing does not have that 'Link' ability.  But I am talking about a regular IDW type Inventor drawing.  I am not 100% sure if this is also the case for DWG type Inventor drawings, because I never use that type.  I believe you can add notes into your drawing that can point to a parameter in 'the model', but that is not the same thing as 'Linked' parameters.  You can copy a parameter or parameter value between the two documents, but that will not remain 'Linked', and you may need a way to keep it updated, if that is important.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

You can 'retrieve' model dimensions into your drawing views, which can then be used to directly modify the model, if you have that setting turned on in your Application Options > Drawing tab, called "Enable part modification from within drawings".  I don't know if that is something that may help out or not though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 13

jgomis.ext
Enthusiast
Enthusiast

I'm interested if you know how to copy the parameter value of an iam in an other parameter in my dwg.

I have created in my iam a form to save and replace the name of all the components and files that I used, and I would like to keep the name the of my components to replace them in the drawing view.

For the moment i'm only at the step where I would like to transfer all the name that I complete in my iam in my dwg.

So for exemple I complete the name of the first part in the iam, param "firstpartnameiam" = "Hello" and I have in my dwg a param called "firstpartnamedwg" = "", and I would like to transfer the value of the parameter that I have in the iam in the dwg.

 

I don't know if I explain well, I'm not really good in english 🙂

0 Likes
Message 6 of 13

WCrihfield
Mentor
Mentor

Hi @jgomis.ext.  I think I may understand the main thing that you want to be able to accomplish with this.  When you make a drawing for your assembly, you include views of each component in the assembly, but not as a view of the assembly with all other components turned off, but as individual, direct linked model files.  And you want to be able to loop through all of those 'referenced models' in your drawing and replace those file references with the new files, so the views will be pointing to the new copied files, right?  And you are planning on storing each model file's path/name within user parameters in the drawing as you are doing the copy & replace process to help keep track of them, so it will know how to find the new files right?  So, are you wanting the code to start from the drawing side, then attempt to access the assembly's parameters and copy them over, or are you wanting the code to start from the assembly side, then try to access the drawing, then copy its parameters to the drawing, or do you just want code that will create the parameters as needed in the drawing to store the values as your other process is running?

 

I do not use that copy/replace type routine myself, but I think I would much rather just replace each referenced model file in the drawing at the same time I am doing the assembly level process, so I don't have to store those file names that way.  When I am replacing file references, I generally like to have a two factor list of Strings, with the first String in the pair being the 'old' FullFileName, and the second String in the pair being the 'new' FullFileName.  This can be done with a NameValueMap or Dictionary(Of String, String) type variables pretty easily, but those are difficult to just store someplace, so I just send them to another rule that loops through the list and does the file reference replacements for me.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 13

jgomis.ext
Enthusiast
Enthusiast

Thanks for your response !

You help me a lot by the past 🙂

 

I would like to start the code from the iam if possible ! Like that I could save the new name only when I started my ilogic code for the save and replace.

0 Likes
Message 8 of 13

jgomis.ext
Enthusiast
Enthusiast

I think I understand what you say in the second part of your message, but if you have a little example, that could help me a lot I think 🙂

0 Likes
Message 9 of 13

WCrihfield
Mentor
Mentor

Hi @jgomis.ext.  Below is a fairly simple example of an iLogic rule that could be ran from the assembly to copy user parameters from itself to that assembly's drawing.  In this simple example, I am just assuming that your drawing file has the ".idw" file extension, and not the ".dwg" file extension, but you can easily change that if needed.  Then I am also assuming that the drawing file is saved in the same location as the assembly, and has the same file name as the assembly, but with the different file extension, which is common practice.  If that is not the case, then you will have to change that part of the code to specify the drawing's FullFileName as needed, because there is no reference from the assembly's side to the drawing, only one from the drawing side to the assembly.  Then I get the UserParameters collection from both the assembly and the drawing to variables.  Then I loop through the assembly's user parameters, checking for which ones to copy to the drawing.  Since I don't know which ones, I am just showing a couple of suggestions for how this could be set-up, without needing to 'hardcode' those parameter names into the code.  One method would be to check the checkbox in the Export column of the Parameters dialog for each one you want to copy to the drawing.  But if you use that for other purposes, that may not be a good option for you.  Another idea (the one shown in the code) is to include some specific text (case sensitive & spelling sensitive) in the 'Comment' of each user parameter you want to copy to the drawing.   When parameters like that are found by the code, they get copied to the drawing.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oAsmUParams As UserParameters = oADoc.ComponentDefinition.Parameters.UserParameters

Dim oDrawingFile As String = System.IO.Path.ChangeExtension(oADoc.FullFileName, ".idw") 'or ".dwg"
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False) 'False = invisible
Dim oDrgUParams As UserParameters = oDDoc.Parameters.UserParameters

For Each oAsmUParam As UserParameter In oAsmUParams
	'If oAsmUParam.ExposedAsProperty Then 'copy to drawing (same as Export checkbox in dialog)
	If oAsmUParam.Comment.Contains("COPY TO DRAWING") Then '<<< CHANGE AS NEEDED
		Dim oDrgUParam As UserParameter = Nothing
		Try 'try to find that parameter in the drawing
			oDrgUParam = oDrgUParams.Item(oAsmUParam.Name)
		Catch 'it was not found, so try to create it in the drawing
			oDrgUParam = oDrgUParams.AddByValue(oAsmUParam.Name, oAsmUParam.Value, oAsmUParam.Units)
		End Try
	End If
Next
If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
'If oDDoc.Dirty Then oDDoc.Save2(False)

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 13

WCrihfield
Mentor
Mentor

@jgomis.ext 

And here is a partial example of the process I was talking about with storing the 'old' FullFileName, and the 'new' FullFileName pairs in a 2-factor list, for use later, to avoid all of the user parameter stuff.  This is not a complete working sample.  It is just showing the main parts, because I have no idea how all of your existing code is currently laid out (and I don't really want to 😅), so I am attempting to show you how to modify any code you may already have to adapt this process into it.  I prepared two iLogic rules in this example.  The first rule represents your current code for copying and replacing stuff in your assembly, but does not include any code for that process, just code for the process I'm talking about.  The second rule represents the one that would be used to replace all of the model file references in it, based on the data you gathered in the first rule.  The first rule gathers the needed data into a dictionary, then sends that data to the second rule.  The second rule looks for that data when it starts, and if received, it will start to loop through that data to replace file references in the drawing.  The second rule currently does not know what to do if it does not receive that list of data at the beginning, so it will just exit the rule if that happens.

Here is the first rule example:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
'create this dictionary 'before' the loop, not inside of the loop
Dim oFileRefPairs As New Dictionary(Of String, String)

'your other code to copy & replace components or file references in the assembly
'as you determine what the 'old' FullFileName was, and the 'new' FullFileName will be
'store them in this dictionary, like this:
If oFileRefPairs.Keys.Contains(sOldFullFileName) = False Then
	oFileRefPairs.Add(sOldFullFileName, sNewFullFileName)
End If

'then when done filling that dictionary with the file pairs, send that dictionary to another rule
'the other rule you sent it to will recieve it, then use it to process the drawing by looping through its contents

Dim oDrawingFile As String = System.IO.Path.ChangeExtension(oADoc.FullFileName, ".idw") 'or ".dwg"
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False) 'False = invisible

Dim sOtherRuleName As String = "Replace Drawing Model References"
Dim oArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oArgs.Add("Target Document", oDDoc)
oArgs.Add("File Reference Pairs", oFileRefPairs)
iLogicVb.Automation.RunExternalRuleWithArguments(oDDoc, sOtherRuleName, oArgs)

If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
'If oDDoc.Dirty Then oDDoc.Save2(False)

...and below is the second rule example:

Dim oDDoc As DrawingDocument = Nothing
If RuleArguments.Exists("Target Document") Then
	oDDoc = RuleArguments.Value("Target Document")
Else
	oDDoc = ThisDoc.Document
End If

Dim oFileRefPairs As Dictionary(Of String, String)
If RuleArguments.Exists("File Reference Pairs") Then
	oFileRefPairs = RuleArguments.Value("File Reference Pairs")
Else
	'NO LIST OF FILE PAIRS WAS RECEIVED
	'oFileRefPairs = New Dictionary(Of String, String) 'will be empty
	'<!!! you may have to do things differently if this is the case !!!>
	Exit Sub
End If

Dim oFDs As FileDescriptorsEnumerator = oDDoc.File.ReferencedFileDescriptors
If oFDs.Count = 0 Then Exit Sub 'no file references to process
For Each oFD As FileDescriptor In oFDs
	If oFD.ReferenceMissing Or oFD.ReferenceDisabled Then Continue For
	For Each oFileRefPair In oFileRefPairs
		If oFD.FullFileName = oFileRefPair.Key Then 'Key is 'old' FullFileName
			Try
				oFD.ReplaceReference(oFileRefPair.Value) 'Value is 'new' FullFileName
			Catch
				'what to do when that fails, maybe Logger.Error()
			End Try
		End If
	Next 'oFileRefPair
Next 'oFD
If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
'If oDDoc.Dirty Then oDDoc.Save2(False)

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 13

jgomis.ext
Enthusiast
Enthusiast

Thanks so much !

For the first part it's working well exept these lines :

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oAsmUParams As UserParameters = oADoc.ComponentDefinition.Parameters.UserParameters
Dim sFilePath As String = "\\intranet-reel.fr\workspace\_ISCP\04_INDUSTRIALIZATION\407_Development\H23003 - Tambours Levage\OUTIL DE PARAMETRISATION DU TAMBOUR\02-Outil de paramétrisation\"

Dim oDrawingFile As String = System.IO.Path.ChangeExtension(sFilePath & "Plan_automatique", ".dwg")
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False) 'False = invisible
Dim oDrgUParams As UserParameters = oDDoc.Parameters.UserParameters

For Each oAsmUParam As UserParameter In oAsmUParams
	'If oAsmUParam.ExposedAsProperty Then 'copy to drawing (same as Export checkbox in dialog)
	If oAsmUParam.Comment.Contains("COPY TO DRAWING") Then '<<< CHANGE AS NEEDED
		Dim oDrgUParam As UserParameter = Nothing
		Try 'try to find that parameter in the drawing
			oDrgUParam = oDrgUParams.Item(oAsmUParam.Name)
		Catch 'it was not found, so try to create it in the drawing
			oDrgUParam = oDrgUParams.AddByValue(oAsmUParam.Name, oAsmUParam.Value, oAsmUParam.Units)
		End Try
	End If
Next

If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
oDDoc.save
oDDoc.Close

 

I have already create the name of the parameters in my drawing and I just like to import the value. But don't find how to import these value.

It's working with the "Catch" to create the parameters if it's not exist and the value is import well but in my case I just want that the try work well.

If you where is my problem thanks you 🙂 

0 Likes
Message 12 of 13

jgomis.ext
Enthusiast
Enthusiast

I would like to have something like this but I'm not very well in coding 🙂

 

For Each oAsmUParam As UserParameter In oAsmUParams
	'If oAsmUParam.ExposedAsProperty Then 'copy to drawing (same as Export checkbox in dialog)
	If oAsmUParam.Comment.Contains("COPY TO DRAWING") Then '<<< CHANGE AS NEEDED
		Dim oDrgUParam As UserParameter = Nothing
		If oDrgUParam.Name = oDrgUParams.Item(oAsmUParam.Name) Then
			oDrgUParam = oAsmUParam.Value
		Else
			oDrgUParam = oDrgUParams.AddByValue(oAsmUParam.Name, oAsmUParam.Value, oAsmUParam.Units)
		End If
	End If
Next
0 Likes
Message 13 of 13

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jgomis.ext.  You are right, my code was not complete, because when it found the existing user parameter, it was not then setting its value.  It would only set the value of the new user parameters that it created.  Sorry about that.

Since you do not want the code to create any user parameters, just copy values to already existing user parameters, I deleted that line of code that created one, and replaced it with a Logger.Error() line to give some feedback within the iLogic Log window (in the background, no pop-ups), but then I commented that line out too, in case you did not want that either.  I put the value assigning part just after the Try...Catch block, so that the Try will only fail if the parameter is not found.  I can't think of any reason just that part would fail, so it does not need to be in a Try block.  But I worded the value part so that it will only attempt to set a value if the parameter was found, and its current value does not already match the assembly parameter's value.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oAsmUParams As UserParameters = oADoc.ComponentDefinition.Parameters.UserParameters
Dim sFilePath As String = "\\intranet-reel.fr\workspace\_ISCP\04_INDUSTRIALIZATION\407_Development\H23003 - Tambours Levage\OUTIL DE PARAMETRISATION DU TAMBOUR\02-Outil de paramétrisation\"

Dim oDrawingFile As String = sFilePath & "Plan_automatique.dwg"
Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDrawingFile, False) 'False = invisible
Dim oDrgUParams As UserParameters = oDDoc.Parameters.UserParameters

For Each oAsmUParam As UserParameter In oAsmUParams
	If oAsmUParam.Comment.Contains("COPY TO DRAWING") Then '<<< CHANGE AS NEEDED
		Dim oDrgUParam As UserParameter = Nothing
		Try 'try to find that parameter in the drawing
			oDrgUParam = oDrgUParams.Item(oAsmUParam.Name)
		Catch 'it was not found
			'Logger.Error("Drawing UserParameter named '" & oAsmUParam.Name & "' was not found.")
		End Try
		If oDrgUParam IsNot Nothing AndAlso oDrgUParam.Value <> oAsmUParam.Value Then
			oDrgUParam.Value = oAsmUParam.Value
		End If
	End If
Next
If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
oDDoc.Save
oDDoc.Close

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

EESignature

(Not an Autodesk Employee)

0 Likes