Set part material from a Windows Form (DLL)

Set part material from a Windows Form (DLL)

JBerns
Advisor Advisor
1,867 Views
15 Replies
Message 1 of 16

Set part material from a Windows Form (DLL)

JBerns
Advisor
Advisor

Dear Community,

 

I would like to set part materials from a Windows Form (DLL).

2021-08-11_9-31-20.png

 

Using code from this IMAGINiT blog, iLogic Forms - Native or .Net, I can edit the dimensions of the parts.

 

The DLL contains the lines:

Public Parameter As IParamDynamic
Public iLogicVB As ILowLevelSupport

The rule contains the lines:

myForm.Parameter = Parameter
myForm.iLogicVB = iLogicVb

 

I thought to access iproperties (materials), I should add these lines to the DLL and the rule respectively:

Public iProps As IiProperties

myForm.iProps = IiProperties

However, this error appears when the rule is run:

 

          Error on Line 29 : 'IiProperties' is a type and cannot be used as an expression.

 

I hope someone may have knowledge or experience on how to change part material from a DLL.

I have not been able to find any examples online for the IiProperties Autodesk Help - iLogic - IiProperties Interface 

FYI - The rule will be run from an assembly.

 

I look forward to your replies.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (3)
1,868 Views
15 Replies
Replies (15)
Message 2 of 16

JBerns
Advisor
Advisor

First error solution was easy.

Changed the line in the rule to:

 

myForm.iProps = iProps

 

However, I cannot set the material of a part from the Windows Form.

I tried this:

 

iProps.Material("Box_2020-03-12_01:1") = "CUSTOM01"

 

but the IiProperties.Material requires an object as its argument.

 

How would you pass the part as an object to the Windows Form or how can the Form get the part as an object?

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 3 of 16

Michael.Navara
Advisor
Advisor
Accepted solution

Hi,

In attachment is my approach to edit material in selected part from assembly.

In src subfolder is complete source code for DLL

In out subfolder are files

  • iLogicPartEditor.dll - Compiled DLL. Copy this to <InventorInstallFolder>\Bin\iLogicAdd
  • EditMaterial.iLogicVb - Rule for edit material using iLogicPartEditor.dll
  • EditMaterialComplete.iLogicVb - complete code (including editor form definition. No DLL is needed)

I hope it helps you 😉

Message 4 of 16

JhoelForshav
Mentor
Mentor

Hi @JBerns 

The interface IiProperties provides the same properties as writing iProperties in iLogic.

iProperties.Material(componentOrDocName) takes a string as argument so that should work. Are you sure you just didn't forget to update the assembly once you've changed the material? In that case you'll not see any result until you close the form and update.

I made a quick and very simple screencast to demonstrate changing material of a part in an assembly this way 🙂

 

Message 5 of 16

JBerns
Advisor
Advisor

@Michael.Navara,

Thank you for the great examples. I was able to get those to work with Inventor 2021.

My apologies though. I should have mentioned I am still on Inventor 2019.

 

I discovered that my VS project did not have a reference to Autodesk.Inventor.Interop.dll. Hadn't needed it.

I added it. It is Version 23.0.0.0.

 

VS code now begins with this:

Imports System.Windows.Forms
Imports Autodesk.iLogic.Interfaces
Imports System.Drawing
Imports inventor

Public Class iLogic
    Public Parameter As IParamDynamic
    Public iLogicVB As ILowLevelSupport
    Public iProps As IiProperties

 

References include:

2021-08-12_8-53-33.png

Despite these changes and then rebuilding the solution (dll), iProps reports as Nothing, therefore it can not change the material.

 

2021-08-12_7-52-50.png

 

I will keep investigating and try to apply the code to 2019.

 

 

Regards,

Jerry

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 6 of 16

JBerns
Advisor
Advisor

@JhoelForshav,

Another great example I will try to get to work in Inventor 2019.

Yes, I do have an update statement after the iProps.Material line, but the error happens on that line stating that iProps is Nothing.

 

Something is not referenced correctly in my VS project. I will keep searching.

 

Attachments:

EditFourBlocks... = The VS project code

VBNetFormForiLogic... = Inventor documents

 

Thanks for your assistance. Much appreciated.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 7 of 16

JhoelForshav
Mentor
Mentor

Hi Jerry! The IiProperties interface is called iProperties in iLogic.

Try changing the line:

myForm.iProps = iProps 'IiProperties

in your iLogic rule to:

myForm.iProps = iProperties

Then iProps in your form wont be nothing anymore 🙂

Message 8 of 16

JhoelForshav
Mentor
Mentor

Replace that line and change dll to update material according to comboboxes and it works like a charm 😄

 

Message 9 of 16

JBerns
Advisor
Advisor

Here are the Autodesk.Inventor.Interop "different versions" conflict messages from the VS Error List window:

 

Warning		Found conflicts between different versions of "Autodesk.Inventor.Interop" that could not be resolved.

There was a conflict between "Autodesk.Inventor.Interop, Version=23.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564" and "Autodesk.Inventor.Interop, Version=23.2.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564".
    "Autodesk.Inventor.Interop, Version=23.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564" was chosen because it was primary and "Autodesk.Inventor.Interop, Version=23.2.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564" was not.
    References which depend on "Autodesk.Inventor.Interop, Version=23.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564" [C:\Program Files\Autodesk\Inventor 2019\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll].
        C:\Program Files\Autodesk\Inventor 2019\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll
          Project file item includes which caused reference "C:\Program Files\Autodesk\Inventor 2019\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll".
            Autodesk.Inventor.Interop, Version=26.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564, processorArchitecture=MSIL
    References which depend on "Autodesk.Inventor.Interop, Version=23.2.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564" [C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Autodesk.Inventor.Interop\v4.0_23.2.0.0__d84147f8b4276564\Autodesk.Inventor.Interop.dll].

 

I am running Inventor 2019, but Inventor 2021 is also installed on this workstation.

 

Does anyone know how to resolve this conflict without installing Inventor 2021?

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 10 of 16

JBerns
Advisor
Advisor

@JhoelForshav,

 

I tried using the DLL from your RAR file, but it too resulted in an error. 

I built a new project using code from your video. That was successful.

 

I think my EditFourBlocks VS project is corrupted.

I have attached a video. I did not use Screencast because I wanted to show editing in VS.

 

At 00:10, you can see that iProps has value. I can access the Material property.

At 00:50, iProps has become Nothing and results in an error.

 

Very peculiar. I will try to rebuild the project from scratch.

Thank you, everyone, for your time and assistance.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 11 of 16

JhoelForshav
Mentor
Mentor
Accepted solution

@JBerns 

Actually, all we can see from 00:10 in your video is that the variable iProps is declared as an object of type IiParameters. That's why intellisense is giving you suggestions. It still doesn't have a value, you've only declared what type of object it is. The value is actually set from the iLogic rule before the form is shown. This specific object of type IiParameters has to be retrieved from iLogic in Inventor (I hope this makes sense), thats why we have to assign the value from the iLogic rule, because in that environment it already exists as iProperties.

Did you change the row of code in the iLogic rule from:

myForm.iProps = iProps 'IiProperties

To:

myForm.iProps = iProperties

If you did, then I'm very confused as to why it doesnt work 🤔

 

In other words, this iLogic rule, in combination with the DLL from my rar-archive should work?

AddReference "C:\Users\jberns\source\repos\EditFourBlocks\EditFourBlocks\bin\Debug\EditFourBlocks.dll"
Private Sub Main()
	
'	Dim oAsmLibMat_Array, oLib_Array1, oLib_Array2, oLib_Array3, oLib_Array4 As New ArrayList
	Dim oAsmLibMat_List As New Generic.List(Of String)
	Dim oAssetLib As AssetLibrary
	Dim oAsset As Asset
	For Each oAssetLib In ThisApplication.AssetLibraries
		'  For Each oAsset In oAssetLib.AppearanceAssets
		If oAssetLib.DisplayName = "NMC Materials" Then
			Dim material As MaterialAsset
			For Each material In oAssetLib.MaterialAssets
'				oAsmLibMat_Array.Add(material.DisplayName)
				oAsmLibMat_List.Add(material.DisplayName)
			Next
		End If
	Next
	
	Dim oLib_List1 = Clone(oAsmLibMat_List)
	Dim oLib_List2 = Clone(oAsmLibMat_List)
	Dim oLib_List3 = Clone(oAsmLibMat_List)
	Dim oLib_List4 = Clone(oAsmLibMat_List)


	Using myForm As New EditFourBlocks.iLogic
		myForm.Parameter = Parameter
		myForm.iLogicVB = iLogicVb
		myForm.iProps = iProperties 'IiProperties
		'myForm.oPartDoc = oPartDoc

		myForm.oBox1Length = Parameter("Box_2020-03-12_01:1", "Length")
		myForm.oBox1Width  = Parameter("Box_2020-03-12_01:1", "Width")
		myForm.oBox1Height = Parameter("Box_2020-03-12_01:1", "Height")
		
		myForm.oBox2Length = Parameter("Box_2020-03-12_02:1", "Length")
		myForm.oBox2Width  = Parameter("Box_2020-03-12_02:1", "Width")
		myForm.oBox2Height = Parameter("Box_2020-03-12_02:1", "Height")
		
		myForm.oBox3Length = Parameter("Box_2020-03-12_03:1", "Length")
		myForm.oBox3Width  = Parameter("Box_2020-03-12_03:1", "Width")
		myForm.oBox3Height = Parameter("Box_2020-03-12_03:1", "Height")
		
		myForm.oBox4Length = Parameter("Box_2020-03-12_04:1", "Length")
		myForm.oBox4Width  = Parameter("Box_2020-03-12_04:1", "Width")
		myForm.oBox4Height = Parameter("Box_2020-03-12_04:1", "Height")

		' Define the material lists
		Dim Mats01 As New Generic.List(Of String)
		Dim Mats02 As New Generic.List(Of String)
		Dim Mats03 As New Generic.List(Of String)
		Dim Mats04 As New Generic.List(Of String)

		Mats01 = oLib_List1
		Mats02 = oLib_List2
		Mats03 = oLib_List3
		Mats04 = oLib_List4
				

		' Define part names
		Dim sPartNm1 As String = "Box_2020-03-12_01.ipt"
		Dim sPartNm2 As String = "Box_2020-03-12_02.ipt"
		Dim sPartNm3 As String = "Box_2020-03-12_03.ipt"
		Dim sPartNm4 As String = "Box_2020-03-12_04.ipt"
		
'		' Define materials
'		Dim strMaterialNm1 As String = "HR"
'		Dim strMaterialNm2 As String = "304 SS"
'		Dim strMaterialNm3 As String = "ALUM-6061"
'		Dim strMaterialNm4 As String = "UHMW, GREEN"	
		
		' Get the active assembly.
		Dim oAsmDoc As AssemblyDocument
		oAsmDoc = ThisApplication.ActiveDocument

		' Change material
		Dim oDoc As Document
		For Each oDoc In oAsmDoc.AllReferencedDocuments
			' Check to see if this is a part
			If oDoc.DocumentType = kPartDocumentObject Then
				Dim oPartDoc2 As PartDocument
				oPartDoc2 = oDoc
				Select Case oPartDoc2.DisplayName
					Case sPartNm1
						'myForm.oBox1Matl = strMaterialNm1
						myForm.oBox1Matl = iProperties.MaterialOfComponent("Box_2020-03-12_01:1")
						oLib_List1 = AddPartDocMatls2(oPartDoc2, oLib_List1)
						oLib_List1.Sort
						myForm.oBox1Doc = oDoc
					Case sPartNm2
						'myForm.oBox2Matl = strMaterialNm2
						myForm.oBox2Matl = iProperties.MaterialOfComponent("Box_2020-03-12_02:1")
						oLib_List2 = AddPartDocMatls2(oPartDoc2, oLib_List2)
						oLib_List2.Sort
						myForm.oBox2Doc = oDoc
					Case sPartNm3
						'myForm.oBox3Matl = strMaterialNm3
						myForm.oBox3Matl = iProperties.MaterialOfComponent("Box_2020-03-12_03:1")
						oLib_List3 = AddPartDocMatls2(oPartDoc2, oLib_List3)
						oLib_List3.Sort
						myForm.oBox3Doc = oDoc
					Case sPartNm4
						'myForm.oBox4Matl = strMaterialNm4
						myForm.oBox4Matl = iProperties.MaterialOfComponent("Box_2020-03-12_04:1")
						oLib_List4 = AddPartDocMatls2(oPartDoc2, oLib_List4)
						oLib_List4.Sort
						myForm.oBox4Doc = oDoc
				End Select
			End If
		Next
	Break
	
		myForm.oBox1MatList = oLib_List1
		myForm.oBox2MatList = oLib_List2
		myForm.oBox3MatList = oLib_List3
		myForm.oBox4MatList = oLib_List4
	
	
		' Show the dialog
		myForm.ShowDialog
		
		' if user cancelled, do nothing
		If myForm.didCancel Then Return

		' pass the form values back to Inventor
		RuleParametersOutput()
		Parameter("Box_2020-03-12_01:1", "Length") = myForm.oBox1Length
		Parameter("Box_2020-03-12_01:1", "Width")  = myForm.oBox1Width
		Parameter("Box_2020-03-12_01:1", "Height") = myForm.oBox1Height
		                                           
		Parameter("Box_2020-03-12_02:1", "Length") = myForm.oBox2Length
		Parameter("Box_2020-03-12_02:1", "Width")  = myForm.oBox2Width
		Parameter("Box_2020-03-12_02:1", "Height") = myForm.oBox2Height
		                                           
		Parameter("Box_2020-03-12_03:1", "Length") = myForm.oBox3Length
		Parameter("Box_2020-03-12_03:1", "Width")  = myForm.oBox3Width
		Parameter("Box_2020-03-12_03:1", "Height") = myForm.oBox3Height
		                                           
		Parameter("Box_2020-03-12_04:1", "Length") = myForm.oBox4Length
		Parameter("Box_2020-03-12_04:1", "Width")  = myForm.oBox4Width
		Parameter("Box_2020-03-12_04:1", "Height") = myForm.oBox4Height
		
	'	Parameter("Box1Length") = myForm.oBox1Length
	'	Parameter("Box1Width")  = myForm.oBox1Width
	'	Parameter("Box1Height") = myForm.oBox1Height

	'	Parameter("Box2Length") = myForm.oBox2Length
	'	Parameter("Box2Width")  = myForm.oBox2Width
	'	Parameter("Box2Height") = myForm.oBox2Height
		
	'	Parameter("Box3Length") = myForm.oBox3Length
	'	Parameter("Box3Width")  = myForm.oBox3Width
	'	Parameter("Box3Height") = myForm.oBox3Height
		
	'	Parameter("Box4Length") = myForm.oBox4Length
	'	Parameter("Box4Width")  = myForm.oBox4Width
	'	Parameter("Box4Height") = myForm.oBox4Height

	'	MsgBox("Box1 material: " & myForm.cboBox1Matl.selectedtext)
		
		InventorVb.DocumentUpdate()

	End Using

End Sub


Private Function AddPartDocMatls2(LocPartDoc As PartDocument, MatList As Generic.List(Of String))
'	Dim TempMatList As Generic.List(Of String) = MatList
	Dim oPartAsset As Asset
	For Each oPartAsset In LocPartDoc.MaterialAssets			
		'Add material to list if it is not already in the list
'		If Not TempMatList.Contains(oPartAsset.DisplayName) Then
'			TempMatList.Add(oPartAsset.DisplayName)
'		End If
		If Not MatList.Contains(oPartAsset.DisplayName) Then
			MatList.Add(oPartAsset.DisplayName)
		End If
	Next
	Return MatList
End Function

Public Function Clone(listToClone As List(Of String)) As List(Of String)
	Return listToClone.Select(Function(s)(s.Clone())).Cast(Of String).ToList()
End Function

 

Message 12 of 16

JBerns
Advisor
Advisor

@JhoelForshav,

 

Once again you have found the issue in the code, but now I am confused.

 

So, you are saying that within iLogic, the iProperties object is already known? That makes sense.

I get that we have to retrieve or pass the information from the rule to the DLL, but I was confused by the name mismatch.

 

2021-08-13_7-42-10.png

 

I had success by matching the first two objects (Parameter and iLogicVB). I was certain I had to do the same for iProps and IiProperties.

 

The code is working. YEAH! No corruption. No rebuild. Great way to end the week.

 

Thanks again, Jhoel, for your diligence in getting the solution explained to me. 😉

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 13 of 16

JhoelForshav
Mentor
Mentor

@JBerns 

I'm happy to hear it works for you now 🙂

You can call the variables in your dll-class whatever you want. As long as you set their values to the objects exposed by iLogic 🙂 I get the confusion but in the dll-class these names are just what you choose to call your variables meant to hold the ilogic objects. The same way as with any variable you declare, you choose its name.

Message 14 of 16

JBerns
Advisor
Advisor

@JhoelForshav / @Michael.Navara / @johnsonshiue ,

 

I am now trying to edit the sheet metal styles (rule) of components in the assembly from the DLL.

JBerns_1-1705509814389.png

 

Your previous assistance and code was able to populate the ComboBox list with available sheet metal styles and the component's current sheet metal style.

 

I found some info on ISheetMetal.SetActiveStyle Method, but that does not seem to work with assembly components.

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=54a41864-4528-d8d7-af92-b13f1105b75a 

I think that code can only work with a sheet metal part document and the part cannot be a multibody.

 

Do you or anyone on the forum know how to change an assembly component's sheet metal style (rule) from a DLL?

 

Perhaps I can only change the sheet metal style after the dialog is closed and code execution returns to the iLogic rule.

 

Thanks for your time and attention. I look forward to your replies.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 15 of 16

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @JBerns 

I think there are multiple ways to achieve this, but I'll try to give an example that is the in line with how this project is built.

Add the interface ICadComponent to your dll:

Public Component As ICadComponent

Then in your iLogic rule, set the value of Component in the dll:

myForm.Component = Component

 
Then when you wanna set the Sheet Metal Style of a component, use this:

Component.InventorComponent("componentname").Definition.SheetMetalStyles.Item("sheetmetalstylename").Activate

 I dont have your files so I haven't been able to test it, but it should work. Let me know otherwise 🙂

Message 16 of 16

JBerns
Advisor
Advisor

@JhoelForshav,

 

BRILLIANT!

EditFourBlocks plus SheetMetal Parts.gif

I would have spent a long time figuring out that last line of code. 

 

Thank you very much, Jhoel!

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes