Showing results for 
Search instead for 
Do you mean 
Reply

Import part family items from Excel file

Hi everyone!

I would like to import list of part family items from excel sheet into rulestream subpart
For example: i have a subpart called "Breakers" with some breakers inside it. Breaker has properties: "Voltage", "Manfacturer" and "Number of inputs"

User wants to fill breakers subpart with an Excel sheet, containing these parameters. How can i do that?

I add file import dialog and file import spec into my model, but when i chose file nothing happens. How can i access to chosen file?

Rulestream ETO Architect 2017-02-10 10.02.23.png

 

I know how to read an excel file with VB code, and i can parse it as list of breaker objects. How to fill my subpart with that list ?

Maybe there is more simple way?

3 REPLIES

Re: Import part family items from Excel file


I know how to read an excel file with VB code, and i can parse it as list of breaker objects. How to fill my subpart with that list ?

Maybe there is more simple way?


You need to create this list, loop through each object of it, add each object to the target SubpartColllection.

Something like this:

 

RootPart.Subparts("<subpart collection name").Add(<object>)
Highlighted

Re: Import part family items from Excel file

Yuri, thanks for your reply

Unfortunately, that's not gonna work, because 

RootPart.Subparts("Name")

gives you a Subpart object, which doesn't have .Add(object) overload. 

Instead, ther is .Add(Part opart, string strName) and .Add2(Part opart, string strName) methods in Subpart object. I already tried to use it - the problem is that you need to make Part object from your custom object.

First thing i thought was to inherit from Part object, and i came up with this code

	Public Class Breaker
		Inherits Part ' Inherit from Part
		Public PartNumber As String
		Public Voltage As Double
		Public Manfacturer As String
		Public NumberOfInputs As Long
	End Class

	Public Sub FillBreakers(ByRef line_up As Part)
		Dim breakers = New List(Of Breaker) From { _
			New Breaker() With {.PartNumber = "1", .Manfacturer = "Test", .Voltage = 1000, .NumberOfInputs = 2}, _
			New Breaker() With {.PartNumber = "2", .Manfacturer = "Test2", .Voltage = 2000, .NumberOfInputs = 4}, _
			New Breaker() With {.PartNumber = "3", .Manfacturer = "Test3", .Voltage = 3000, .NumberOfInputs = 6}}

		Dim sp As Subpart = line_up.Subparts("Breakers")

		Dim count = 1
		For Each breaker As Breaker In breakers
			sp.Add(breaker, String.Format("Breaker:{0}", count))
			count += 1
		Next
	End Sub

When i run this code, it fills subpart with three of these breakers, but they doesn't contain any properties

Rulestream ETO Engineer 2017-02-16 14.59.02.png

 

Re: Import part family items from Excel file

Folks, i finally found a way to programmatically add part to existing supbart, that was actually pretty easy

look for the code below to see

Public Class Breaker
	Public PartNumber As String
	Public Voltage As Double
	Public Manfacturer As String
	Public NumberOfInputs As Long
End Class

Public Sub FillBreakers(ByRef line_up As Part)
	Dim breakers = New List(Of Breaker) From { _
		New Breaker() With {.PartNumber = "1", .Manfacturer = "Test1", .Voltage = 1000, .NumberOfInputs = 2}, _
		New Breaker() With {.PartNumber = "2", .Manfacturer = "Test2", .Voltage = 2000, .NumberOfInputs = 4}, _
		New Breaker() With {.PartNumber = "3", .Manfacturer = "Test3", .Voltage = 3000, .NumberOfInputs = 6}}

	Dim sp As Subpart = line_up.Subparts("Breakers")

	sp.removeAll()

	For Each breaker As Breaker In breakers
		Dim part = sp.AddPart("Breaker") 'creating part instance

		' fill its properties with our values
		part.Properties.GetItemByKey("PartNumber").SetInputValue(breaker.PartNumber)
		part.Properties.GetItemByKey("Manfacturer").SetInputValue(breaker.Manfacturer)
		part.Properties.GetItemByKey("Voltage").SetInputValue(breaker.Voltage)
		part.Properties.GetItemByKey("NumberOfInputs").SetInputValue(breaker.NumberOfInputs)
	Next
End Sub

As you can see, there is an API, that provides a way to create an instance of part and it gives you a refference to it, so you can set your properties whatever you need

 

So, the remaining question is - how to access file, which is selected via file import dialog?