Code for moving same part files to different positions using a text file

Hi

I have an assembly file with 4 parts of same name as (1.prt).I would like move them using a Text file where i have 4 lines as such denoting their respective positions in X,Y and Z.

100,0,0
-8000,1000,0
1000,1000,0
1000,1000,0

I am missing by the code a small bit linking the text file with the component,meaning when first line of the text file is read it has to move the 1 out of 4 parts by 1000,0,0 and so on.

please find the below code and kindly do the changes to it.

For i As integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(i)

line = ReadLine(j+1, allLines)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)
Next

The problem with the above code it is just reading the first line and moving all the selected parts(4 Nos)by 1000,0,0.
But i need only the 1st one to be moved by 1000,0,0 and the second one by -8000,1000,0...

Please suugest

"The problem with the above code it is just reading the first line"

In the for loop, the counter variable is "i", but in the ReadLine command you reference variable "j". I'd guess that you should be using "i" instead of "j"; or, if your intention is to use the "j" variable, you will need to increment it accordingly in the loop.

Hi
I had used i for incrementing the part number and j for the rows in the text file.

If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim line As String
Dim firstPass as Boolean = True
Dim delim As Char() = {","}
Dim reader As New System.IO.StreamReader(openFileDialog1.FileName)
Dim allLines As List(Of String) = New List(Of String)
Dim j as String
Do While Not reader.EndOfStream
allLines.Add(reader.ReadLine())
Loop
reader.Close()

For i As Integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(inx)

line = ReadLine(j, allLines)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)
Next

'avoid problems if the part contains wave links or other interpart data
theSession.UpdateManager.InterpartDelay = True
'reset interpart delay to original value
theSession.UpdateManager.InterpartDelay = myInterpartDelay

End If
End Sub

If i use "j", it gives me an error and if that particular line is replaced with numeric values

line = ReadLine(1, allLines)

It works fine reading the 1 st line in the text file and moving all the selected objectes by X,Y,Z values.
I have a problem with that line alone.

Pseudo Code for that would be as such

for i as integer = 0 to componentList.Count-1

read line(j)

verify input, convert strings to numbers

move component(i) by distance in line(j)

next

Where i would be the part list and j would be the each line in the text file.

Please modify the psudo code and give it to me.

Thanks

The "allLines" variable is a list of type string. It looks like you are reading the text file into this list, which is good. Once all the lines are read into the list, you can access the desired line with the .Item property of the list. Line 1 would be allLines.Item(0), line 2 would be allLines.Item(1), etc. Assuming that line 1 corresponds with the first component, line 2 with the 2nd component, etc; try changing your "For loop" code to:

For i As Integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(inx)

line = allLines.Item(i)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)
Next

Note that the loop starts with i=0; this will get the first selected component and the first line in the list. When the "Next" line is executed, i will be incremented and the next component and text line will be used. When you run out of selected objects, the loop will end. You may want to add some error checking to make sure that you do not run out of coordinate values before you run out of components (but that can be added later - get the code running first).

Thanks works fine.
Also i tried with as well which even worked.
For i As Integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(i)

line = ReadLine(j+1, allLines)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)

j=j+1
Next

Hi
I have a small change in the code.Now i dont have multiple components ,i have one components to be translated and copied.

I have added one extra line which is as below

For i As Integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(inx)

line = allLines.Item(i)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.componentAssembly.copyComponents({selobjs})

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)
Next

Now the code copies the selected part and translates it by reading the line in the text file.But this is happening only one time .

I need the code to be modified so that i select one part then reads all the lines in the text file say(10 lines) and then copies the parts for each looping(so 10 parts are created) and then all 10 are translated to their respective position.

Please suggest.

Please someone help me with the below problem.I have a raised it earlier as well.

Hi
I have a small change in the code.Now i dont have multiple components ,i have one components to be translated and copied.

I have added one extra line which is as below

For i As Integer = 0 To numsel-1
selobjs = theUI.SelectionManager.GetSelectedObject(inx)

line = allLines.Item(i)

Dim strings As String() = line.Split(",")
pt3.x = Double.Parse(strings(0))
pt3.y = Double.Parse(strings(1))
pt3.z= Double.Parse(strings(2))

workPart.componentAssembly.copyComponents({selobjs})

workPart.ComponentAssembly.MoveComponent(selobjs,pt3, RotMat3)
Next

Now the code copies the selected part and translates it by reading the line in the text file.But this is happening only one time .

I need the code to be modified so that i select one part then reads all the lines in the text file say(10 lines) and then copies the parts for each looping(so 10 parts are created) and then all 10 are translated to their respective position.

Please suggest.