Hi,
Instead of using kinematics, I am trying to create arrangements using move component.
1. I have basic sketch created by expressions. I linked coordinates to the sketch curves.
2. I have to edit the expressions to move the coordinates using the sketch.
3. This expression RHS value I stored in Dwg table.
Step 1: I collect all the coordinates default value to double list (This is where I have issue, after breaking the for loop, In the list all values double(0) is somehow getting modified to the last value of the for loop )
Step 2: Read the table, Create arrangement, Update the sketch, Select the matching coordinate, Move the components.
Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Collections
Imports System.Runtime.InteropServices
Imports NXOpen
Imports NXOpen.GeometricUtilities
Imports NXOpen.UF
Imports NXOpen.Assemblies
Imports NXOpenUI
Imports System.Collections.Generic
Imports System.Threading
Module ArrangementUpdateNew
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim theUI As UI = UI.GetUI
Dim ufs As UFSession = UFSession.GetUFSession
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
lw.Open()
Dim i As Integer = Nothing
Dim j As Integer = Nothing
Dim k As Integer = Nothing
Dim ii As Integer = Nothing
Dim ij As Integer = Nothing
Dim ik As Integer = Nothing
Dim PrimeCsysList As List(Of CoordinateSystem) = New List(Of CoordinateSystem)
Dim SecondaryCsysList As List(Of CoordinateSystem) = New List(Of CoordinateSystem)
Dim GroupList As List(Of ComponentGroup) = New List(Of ComponentGroup)
Dim FromOrginCollection As List(Of Double) = New List(Of Double)
Dim FromXAxisCollection As List(Of Double) = New List(Of Double)
Dim FromYAxisCollection As List(Of Double) = New List(Of Double)
Dim FromOrigin() As Double = Nothing
Dim FromXAxis() As Double = Nothing
Dim FromYAxis() As Double = Nothing
Dim ArrangementTable As Tag = Nothing
Dim Cellvalue As String = Nothing
Dim numRows As Integer = Nothing
Dim numCols As Integer = Nothing
Dim rowTag As Tag = Nothing
Dim colTag As Tag = Nothing
Dim cellTag As Tag = Nothing
Dim ArrangementName As String = Nothing
Dim VariablesTitle As List(Of String) = New List(Of String)
'ArrangementOptions.IndividuallyPositioned
Try
Dim Mycomponent As ComponentAssembly = workPart.ComponentAssembly
UpdateArrangementPosition(Mycomponent.RootComponent, 0)
Catch e As Exception
theSession.ListingWindow.WriteLine("Assembly component Individually Postitioned Option failed : " & e.ToString)
End Try
'Collect all Component group detail and Template coordinate system details
For Each Mygroup As ComponentGroup In workPart.ComponentGroups
If TypeOf Mygroup Is ComponentGroup Then
GroupList.Add(Mygroup)
End If
Next
For Each MyCsys As CoordinateSystem In workPart.CoordinateSystems
If TypeOf MyCsys Is CoordinateSystem Then
If Not MyCsys.Name = "" Then
PrimeCsysList.Add(MyCsys)
End If
End If
Next
'Collect all the Details from Primary CSYS before applying the formula or Else for both Primary and secondary Csys will return the same value
'This is where I have the issue the output in the first for loop is reading coorect after collecting the values_
'When I read it in next For loop the Fromorgin(0), FromXaxis(0), FromYaxis(0) is got modified for all the items_
'in the collection to last known value in first for loop
' Just create 5 Csys in different locations and run the journal you will have two set of different results
For i = 0 To PrimeCsysList.Count - 1
FromOrigin = {PrimeCsysList(i).Origin.X, PrimeCsysList(i).Origin.Y, PrimeCsysList(i).Origin.Z}
FromXAxis = {PrimeCsysList(i).Orientation.Element.Xx, PrimeCsysList(i).Orientation.Element.Xy, PrimeCsysList(i).Orientation.Element.Xz}
FromYAxis = {PrimeCsysList(i).Orientation.Element.Yx, PrimeCsysList(i).Orientation.Element.Yy, PrimeCsysList(i).Orientation.Element.Yz}
FromOrginCollection.Add(FromOrigin(2))
FromXAxisCollection.Add(FromXAxis(2))
FromYAxisCollection.Add(FromYAxis(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOrigin(0) & "," & FromOrigin(1) & "," & FromOrigin(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromXAxis(0) & "," & FromXAxis(1) & "," & FromXAxis(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromYAxis(0) & "," & FromYAxis(1) & "," & FromYAxis(2))
Next
For i = 0 To FromOrginCollection.Count - 1
FromOrigin(2) = FromOrginCollection(i)
FromXAxis(2) = FromXAxisCollection(i)
FromYAxis(2) = FromYAxisCollection(i)
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOrigin(0) & "," & FromOrigin(1) & "," & FromOrigin(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromXAxis(0) & "," & FromXAxis(1) & "," & FromXAxis(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromYAxis(0) & "," & FromYAxis(1) & "," & FromYAxis(2))
Next
' Read expression value from table for each arrangement and update the sketch to move the coordinates and update arrangement
'Dim myTabularNoteTags As New List(Of Tag)
'If FindTabularNotes(myTabularNoteTags, workPart) = 0 Then
' 'no tabular notes to process
' Return
'End If
' For Each tableNote As Tag In myTabularNoteTags
' ufs.Tabnot.AskNmRows(tableNote, numRows)
' ufs.Tabnot.AskNmColumns(tableNote, numCols)
' ufs.Tabnot.AskNthRow(tableNote, 0, rowTag)
' ufs.Tabnot.AskNthColumn(tableNote, 0, colTag)
' ufs.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
' ufs.Tabnot.AskCellText(cellTag, Cellvalue)
' If Cellvalue = "Arrangement List" Then
' ufs.Tabnot.AskNthRow(tableNote, 1, rowTag)
' For j = 1 To numCols - 1
' ufs.Tabnot.AskNthColumn(tableNote, j, colTag)
' ufs.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
' ufs.Tabnot.AskCellText(cellTag, Cellvalue)
' VariablesTitle.Add(Cellvalue)
' Next
' For i = 0 To VariablesTitle.Count - 1
' lw.WriteLine(VariablesTitle(i).ToString)
' Next
' For i = 2 To numRows - 1 Step 1
' ufs.Tabnot.AskNthRow(tableNote, i, rowTag)
' Dim Variables As List(Of String) = New List(Of String)
' For j = 0 To numCols - 1
' ufs.Tabnot.AskNthColumn(tableNote, j, colTag)
' ufs.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
' ufs.Tabnot.AskCellText(cellTag, Cellvalue)
' Variables.Add(Cellvalue)
' Next
' ArrangementName = Variables(0)
' Variables.RemoveAt(0)
' lw.WriteLine(ArrangementName.ToString)
' Dim DefaultArrangement As Assemblies.Arrangement = CType(workPart.ComponentAssembly.Arrangements.FindObject("Default"), Assemblies.Arrangement)
' Dim MyArrangement As Assemblies.Arrangement = Nothing
' Try
' MyArrangement = workPart.ComponentAssembly.Arrangements.Create(DefaultArrangement, ArrangementName)
' MyArrangement.IgnoringConstraints = True
' Catch ex As NXException
' lw.WriteLine(ex.Message)
' End Try
' For iii As Integer = 0 To Variables.Count - 1
' If Variables(iii).ToString = "NA" Then
' Variables(iii) = "0"
' End If
' Next
' If VariablesTitle.Count = Variables.Count Then
' For k = 0 To VariablesTitle.Count - 1
' 'This expression will move the base sketch and move the coordinate system to new location
' Dim expression1 As Expression = CType(workPart.Expressions.FindObject(VariablesTitle(k)), Expression)
' expression1.RightHandSide = Variables(k)
' theSession.Preferences.Modeling.UpdatePending = False
' Next
' For Each MyCsys As CoordinateSystem In workPart.CoordinateSystems
' If TypeOf MyCsys Is CoordinateSystem Then
' If Not MyCsys.Name = "" Then
' SecondaryCsysList.Add(MyCsys)
' End If
' End If
' Next
' For ii = 0 To PrimeCsysList.Count - 1
' Dim Movinggroup As ComponentGroup = Nothing
' Dim fromCsys As CoordinateSystem = PrimeCsysList(ii)
' FromOrigin(2) = FromOrginCollection(ii)
' FromXAxis(2) = FromXAxisCollection(ii)
' FromYAxis(2) = FromYAxisCollection(ii)
' 'Comparing
' Dim toCsys As CoordinateSystem = Nothing
' For ij = 0 To SecondaryCsysList.Count - 1
' If SecondaryCsysList(ij).Name = fromCsys.Name Then
' toCsys = SecondaryCsysList(ij)
' End If
' Next
' Dim toOrigin() As Double = {toCsys.Origin.X, toCsys.Origin.Y, toCsys.Origin.Z}
' Dim toXAxis() As Double = {toCsys.Orientation.Element.Xx, toCsys.Orientation.Element.Xy, toCsys.Orientation.Element.Xz}
' Dim toYAxis() As Double = {toCsys.Orientation.Element.Yx, toCsys.Orientation.Element.Yy, toCsys.Orientation.Element.Yz}
' lw.WriteLine(fromCsys.Name & " : " & FromOrigin(0) & "," & FromOrigin(1) & "," & FromOrigin(2))
' lw.WriteLine(toCsys.Name & " : " & toOrigin(0) & "," & toOrigin(1) & "," & toOrigin(2))
' Dim mtx4Transform(15) As Double
' ufs.Mtx4.CsysToCsys(FromOrigin, FromXAxis, FromYAxis, toOrigin, toXAxis, toYAxis, mtx4Transform)
' 'Extract the rotation matrix and the tranlsation vector
' Dim rotMatrix(8) As Double
' ufs.Mtx4.AskRotation(mtx4Transform, rotMatrix)
' Dim transVec(2) As Double
' ufs.Mtx4.AskTranslation(mtx4Transform, transVec)
' 'Convert array of doubles to vector 3d
' Dim translateVector As Vector3d = New Vector3d(transVec(0), transVec(1), transVec(2))
' 'Convert array of doubles to Matrix3x3
' Dim rotationMatrix As Matrix3x3 = convertToMatrix3x3(rotMatrix)
' 'Find which component group to move based on Primarycsys
' Dim Groupobjects() As NXObject = Nothing
' Try
' For ik = 1 To GroupList.Count - 1
' If GroupList(ik).Name = fromCsys.Name Then
' Movinggroup = GroupList(ik)
' Groupobjects = Movinggroup.GetComponents
' lw.WriteLine(Movinggroup.Name & ": Components Count: " & Groupobjects.Length.ToString)
' End If
' Next
' Catch ex As Exception
' lw.WriteLine(ex.Message)
' GoTo NextGroup
' End Try
' 'Move groups for each arrangement as per Primary to Secondary Csys move
' Dim componentPositioner1 As Positioning.ComponentPositioner
' componentPositioner1 = workPart.ComponentAssembly.Positioner
' componentPositioner1.ClearNetwork()
' MyArrangement = CType(workPart.ComponentAssembly.Arrangements.FindObject(ArrangementName), Assemblies.Arrangement)
' componentPositioner1.PrimaryArrangement = MyArrangement
' componentPositioner1.BeginMoveComponent()
' Dim allowInterpartPositioning1 As Boolean
' allowInterpartPositioning1 = theSession.Preferences.Assemblies.InterpartPositioning
' Dim network1 As Positioning.Network
' network1 = componentPositioner1.EstablishNetwork()
' Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)
' componentNetwork1.MoveObjectsState = True
' Dim nullAssemblies_Component As Assemblies.Component = Nothing
' componentNetwork1.DisplayComponent = nullAssemblies_Component
' componentNetwork1.NetworkArrangementsMode = Positioning.ComponentNetwork.ArrangementsMode.Existing
' componentNetwork1.NonMovingGroupGrounded = True
' componentNetwork1.MoveObjectsState = True
' componentNetwork1.RemoveAllConstraints()
' componentNetwork1.SetMovingGroup(Groupobjects)
' Dim loaded1 As Boolean
' loaded1 = componentNetwork1.IsReferencedGeometryLoaded()
' componentNetwork1.BeginDrag()
' componentNetwork1.DragByTransform(translateVector, rotationMatrix)
' componentNetwork1.EndDrag()
' componentNetwork1.ResetDisplay()
' componentNetwork1.ApplyToModel()
' componentNetwork1.Solve()
' componentPositioner1.ClearNetwork()
' Dim nErrs2 As Integer
' nErrs2 = theSession.UpdateManager.AddToDeleteList(componentNetwork1)
' componentPositioner1.DeleteNonPersistentConstraints()
' componentPositioner1.EndMoveComponent()
' Dim nullAssemblies_Arrangement As Assemblies.Arrangement = Nothing
' componentPositioner1.PrimaryArrangement = nullAssemblies_Arrangement
'NextGroup:
' Next
' Else
' lw.WriteLine("Table has missing Parameters or Parameter Title count and Value count not matching")
' End If
' Next
' End If
' Next
End Sub
Function convertToMatrix3x3(ByVal mtx As Double()) As Matrix3x3
Dim mx As Matrix3x3
With mx
.Xx = mtx(0)
.Xy = mtx(1)
.Xz = mtx(2)
.Yx = mtx(3)
.Yy = mtx(4)
.Yz = mtx(5)
.Zx = mtx(6)
.Zy = mtx(7)
.Zz = mtx(8)
End With
Return mx
End Function
Function FindTabularNotes(ByRef theTabNotes As List(Of Tag), ByVal Mypart As Part) As Integer
Dim tmpTabNote As NXOpen.Tag = NXOpen.Tag.Null
Dim type As Integer
Dim subtype As Integer
Do
ufs.Obj.CycleObjsInPart(Mypart.Tag, UFConstants.UF_tabular_note_type, tmpTabNote)
If tmpTabNote = NXOpen.Tag.Null Then
Continue Do
End If
If tmpTabNote <> NXOpen.Tag.Null Then
ufs.Obj.AskTypeAndSubtype(tmpTabNote, type, subtype)
If subtype = UFConstants.UF_tabular_note_subtype Then
theTabNotes.Add(tmpTabNote)
End If
End If
Loop Until tmpTabNote = NXOpen.Tag.Null
Return theTabNotes.Count
End Function
Sub UpdateArrangementPosition(ByVal comp As Component, ByVal indent As Integer)
For Each child As Component In comp.GetChildren()
If child.GetChildren.Length <> 0 Then
' lw.WriteLine(child.Name)
Try
Dim MyObj(0) As NXObject
MyObj(0) = child
Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
assembliesParameterPropertiesBuilder1.Arrangements = Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositioned
Dim NxObject1 As NXObject
NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
assembliesParameterPropertiesBuilder1.Destroy()
Catch ex As NXException
lw.WriteLine("Failed")
End Try
Else
' lw.WriteLine(child.Name)
Try
Dim MyObj(0) As NXObject
MyObj(0) = child
Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
assembliesParameterPropertiesBuilder1.Arrangements = Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositioned
Dim NxObject1 As NXObject
NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
assembliesParameterPropertiesBuilder1.Destroy()
Catch ex As NXException
lw.WriteLine("Failed")
End Try
End If
UpdateArrangementPosition(child, indent + 1)
Next
End Sub
End Module
No Worries
Hi,
I found the solution to the problem. this issue is with VB.Net. It can't handle multidimensional arrays properly. Either you have to split the data or you have to use jagged arrays.Since my array quantity is less I just split the data.
'' 'Collect all the Details from Primary CSYS before applying the formula or Else for both Primary and secondary Csys will return the same value
Dim FromOriginX As Double = Nothing
Dim FromOriginY As Double = Nothing
Dim FromOriginZ As Double = Nothing
Dim FromOriginXx As Double = Nothing
Dim FromOriginXy As Double = Nothing
Dim FromOriginXz As Double = Nothing
Dim FromOriginYx As Double = Nothing
Dim FromOriginYy As Double = Nothing
Dim FromOriginYz As Double = Nothing
Dim Xcollection As List(Of Double) = New List(Of Double)
Dim Ycollection As List(Of Double) = New List(Of Double)
Dim Zcollection As List(Of Double) = New List(Of Double)
Dim Xxcollection As List(Of Double) = New List(Of Double)
Dim Xycollection As List(Of Double) = New List(Of Double)
Dim Xzcollection As List(Of Double) = New List(Of Double)
Dim Yxcollection As List(Of Double) = New List(Of Double)
Dim Yycollection As List(Of Double) = New List(Of Double)
Dim Yzcollection As List(Of Double) = New List(Of Double)
For i = 0 To PrimeCsysList.Count - 1
FromOriginX = PrimeCsysList(i).Origin.X
FromOriginY = PrimeCsysList(i).Origin.Y
FromOriginZ = PrimeCsysList(i).Origin.Z
FromOriginXx = PrimeCsysList(i).Orientation.Element.Xx
FromOriginXy = PrimeCsysList(i).Orientation.Element.Xy
FromOriginXz = PrimeCsysList(i).Orientation.Element.Xz
FromOriginYx = PrimeCsysList(i).Orientation.Element.Yx
FromOriginYy = PrimeCsysList(i).Orientation.Element.Yy
FromOriginYz = PrimeCsysList(i).Orientation.Element.Yz
Xcollection.Add(FromOriginX)
Ycollection.Add(FromOriginY)
Zcollection.Add(FromOriginZ)
Xxcollection.Add(FromOriginXx)
Xycollection.Add(FromOriginXy)
Xzcollection.Add(FromOriginXz)
Yxcollection.Add(FromOriginYx)
Yycollection.Add(FromOriginYy)
Yzcollection.Add(FromOriginYz)
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOriginX & "," & FromOriginY & "," & FromOriginZ)
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOriginXx & "," & FromOriginXy & "," & FromOriginXz)
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOriginYx & "," & FromOriginYy & "," & FromOriginYz)
Next
For i = 0 To PrimeCsysList.Count - 1
FromOrigin = {Xcollection(i), Ycollection(i), Zcollection(i)}
FromXAxis = {Xxcollection(i), Xycollection(i), Xzcollection(i)}
FromYAxis = {Yxcollection(i), Yycollection(i), Yzcollection(i)}
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromOrigin(0) & "," & FromOrigin(1) & "," & FromOrigin(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromXAxis(0) & "," & FromXAxis(1) & "," & FromXAxis(2))
lw.WriteLine(PrimeCsysList(i).Name & " : " & FromYAxis(0) & "," & FromYAxis(1) & "," & FromYAxis(2))
Next
Regards,
Joe