Quantcast
Channel: NX Journaling - Journaling / NXOpen API
Viewing all articles
Browse latest Browse all 787

Arrangement Creation using Sketch Coordinates.

$
0
0

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.IOImports System.Windows.FormsImports System.DrawingImports System.Drawing.ImagingImports System.CollectionsImports System.Runtime.InteropServicesImports NXOpen
Imports NXOpen.GeometricUtilitiesImports NXOpen.UFImports NXOpen.AssembliesImports NXOpenUI
Imports System.Collections.GenericImports System.Threading 
Module ArrangementUpdateNew
 
    Dim theSession As Session = Session.GetSession()Dim workPart As Part = theSession.Parts.WorkDim displayPart As Part = theSession.Parts.DisplayDim theUI As UI = UI.GetUIDim ufs As UFSession = UFSession.GetUFSessionDim lw As ListingWindow = theSession.ListingWindow 
 
    Sub Main()
        lw.Open()Dim i AsInteger=NothingDim j AsInteger=NothingDim k AsInteger=NothingDim ii AsInteger=NothingDim ij AsInteger=NothingDim ik AsInteger=NothingDim 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(OfDouble)=New List(OfDouble)Dim FromXAxisCollection As List(OfDouble)=New List(OfDouble)Dim FromYAxisCollection As List(OfDouble)=New List(OfDouble)Dim FromOrigin()AsDouble=NothingDim FromXAxis()AsDouble=NothingDim FromYAxis()AsDouble=NothingDim ArrangementTable As Tag =NothingDim Cellvalue AsString=NothingDim numRows AsInteger=NothingDim numCols AsInteger=NothingDim rowTag As Tag =NothingDim colTag As Tag =NothingDim cellTag As Tag =NothingDim ArrangementName AsString=NothingDim VariablesTitle As List(OfString)=New List(OfString) 
        'ArrangementOptions.IndividuallyPositionedTryDim Mycomponent As ComponentAssembly = workPart.ComponentAssembly
            UpdateArrangementPosition(Mycomponent.RootComponent, 0)Catch e As Exception
            theSession.ListingWindow.WriteLine("Assembly component Individually Postitioned Option failed : "& e.ToString)EndTry 
 
        'Collect all Component group detail and Template coordinate system detailsForEach Mygroup As ComponentGroup In workPart.ComponentGroupsIfTypeOf Mygroup Is ComponentGroup Then
                GroupList.Add(Mygroup)EndIfNext 
        ForEach MyCsys As CoordinateSystem In workPart.CoordinateSystemsIfTypeOf MyCsys Is CoordinateSystem ThenIfNot MyCsys.Name=""Then
                    PrimeCsysList.Add(MyCsys)EndIfEndIfNext 
        '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 resultsFor i =0To 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 =0To 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 
 
 
 
    EndSub 
    Function convertToMatrix3x3(ByVal mtx AsDouble())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)EndWith 
        Return mx
 
    EndFunctionFunction FindTabularNotes(ByRef theTabNotes As List(Of Tag), ByVal Mypart As Part)AsInteger 
        Dim tmpTabNote As NXOpen.Tag= NXOpen.Tag.NullDim type AsIntegerDim subtype AsInteger 
        Do
            ufs.Obj.CycleObjsInPart(Mypart.Tag, UFConstants.UF_tabular_note_type, tmpTabNote)If tmpTabNote = NXOpen.Tag.NullThenContinueDoEndIfIf tmpTabNote <> NXOpen.Tag.NullThen
                ufs.Obj.AskTypeAndSubtype(tmpTabNote, type, subtype)If subtype = UFConstants.UF_tabular_note_subtypeThen
                    theTabNotes.Add(tmpTabNote)EndIfEndIfLoop Until tmpTabNote = NXOpen.Tag.NullReturn theTabNotes.Count 
    EndFunction 
    Sub UpdateArrangementPosition(ByVal comp As Component, ByVal indent AsInteger) 
        ForEach child As Component In comp.GetChildren()If child.GetChildren.Length<>0Then' lw.WriteLine(child.Name)TryDim MyObj(0)As NXObject
                    MyObj(0)= child
                    Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
                    assembliesParameterPropertiesBuilder1.Arrangements= Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositionedDim NxObject1 As NXObject
                    NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
                    assembliesParameterPropertiesBuilder1.Destroy()Catch ex As NXException
                    lw.WriteLine("Failed")EndTryElse'  lw.WriteLine(child.Name)TryDim MyObj(0)As NXObject
                    MyObj(0)= child
                    Dim assembliesParameterPropertiesBuilder1 As AssembliesParameterPropertiesBuilder = workPart.PropertiesManager.CreateAssembliesParameterPropertiesBuilder(MyObj)
                    assembliesParameterPropertiesBuilder1.Arrangements= Assemblies.AssembliesParameterPropertiesBuilder.ArrangementOptions.IndividuallyPositionedDim NxObject1 As NXObject
                    NxObject1 = assembliesParameterPropertiesBuilder1.Commit()
                    assembliesParameterPropertiesBuilder1.Destroy()Catch ex As NXException
                    lw.WriteLine("Failed")EndTryEndIf
            UpdateArrangementPosition(child, indent +1)NextEndSub 
EndModule

Viewing all articles
Browse latest Browse all 787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>