Submitted by DHuskic on Fri, 01/02/2015 - 13:43
Forums:
I am attempting to move a component from a csys I created on its solid body to another csys(currently the ABS to try and keep it simple). The 'to' csys can be the ABS, I simply translate the component later. The problem is rotating the component correctly. The solid body is not consistent with the component csys. I have both csys' created and have tried numerous code samples, as well as trying to combine some of those and using combinations of the group. Any help is greatly appreciated as I have ran out of ideas to try.
re: csys to csys matrix
You might find something of interest here:
http://www.eng-tips.com/viewthread.cfm?qid=354338
re: moving component from csys to csys
Try the code below; it assumes the "from" csys belongs to the component that you want to move and the "to" csys belongs to the assembly. The code was tested in NX 8.5 to move a top level component (one owned directly by the assembly, not one nested 2 or more levels deep).
'NXJournaling.com
'January 5, 2015
'Move a component from Csys to Csys
'Journal will prompt user to select a "from" csys and a "to" csys; the "from" csys should belong to the
'component that you want to move.
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Module Module1
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "reposition component"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim fromCsys As CoordinateSystem
Dim toCsys As CoordinateSystem
If SelectCsys("Select 'From' coordinate system", fromCsys) = Selection.Response.Cancel Then
Return
End If
If SelectCsys("Select 'To' coordinate system", toCsys) = Selection.Response.Cancel Then
Return
End If
'calculate Csys to Csys transformation matrix
Dim fromOrigin() As Double = {fromCsys.Origin.X, fromCsys.Origin.Y, fromCsys.Origin.Z}
Dim fromXAxis() As Double = {fromCsys.Orientation.Element.Xx, fromCsys.Orientation.Element.Xy, fromCsys.Orientation.Element.Xz}
Dim fromYAxis() As Double = {fromCsys.Orientation.Element.Yx, fromCsys.Orientation.Element.Yy, fromCsys.Orientation.Element.Yz}
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}
Dim mtx4Transform(15) As Double
theUfSession.Mtx4.CsysToCsys(fromOrigin, fromXAxis, fromYAxis, toOrigin, toXAxis, toYAxis, mtx4Transform)
'extract the rotation matrix and the tranlsation vector
Dim rotMatrix(8) As Double
theUfSession.Mtx4.AskRotation(mtx4Transform, rotMatrix)
Dim transVec(2) As Double
theUfSession.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)
'determine component to move based on the chosen "from" csys
Dim comp As Assemblies.Component = fromCsys.OwningComponent
Dim componentPositioner1 As Positioning.ComponentPositioner = theSession.Parts.Work.ComponentAssembly.Positioner
Dim network1 As Positioning.Network
network1 = componentPositioner1.EstablishNetwork()
Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)
Dim markId2 As Session.UndoMarkId = Nothing
If comp IsNot Nothing Then
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Move Component")
Dim movableObjects() As NXObject = {comp}
componentNetwork1.SetMovingGroup(movableObjects)
componentNetwork1.BeginDrag()
'reposition component from the rotation matrix and translation vector
componentNetwork1.DragByTransform(translateVector, rotationMatrix)
componentNetwork1.EndDrag()
componentNetwork1.ResetDisplay()
componentNetwork1.ApplyToModel()
componentNetwork1.Solve()
End If
theSession.UpdateManager.AddToDeleteList(componentNetwork1)
theSession.UpdateManager.DoUpdate(markId2)
lw.Close()
End Sub
Function SelectCsys(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim title As String = "Select a Coordinate system"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_coordinate_system_type
.Subtype = UFConstants.UF_all_subtype
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
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
End Module
http://nxjournaling.com/content/moving-component-csys-csys
http://www.eng-tips.com/viewthread.cfm?qid=377619
Select component instead of Csys?
How would you suggest modifying this code if I wanted to select the component to move instead of csys to move from? I am having difficulty getting the origin from the component.
re: component origin
If you have a reference to a component object, you can use the .GetPosition method to return the origin and orientation matrix of the component.
Unfolding the bracket
Hey, i have a rectangular bracket of which all the bottom & side edges are operated by Edgeblend feature. I have extracted all the radii of edgeblend. Is it possible to unfold that bracket ? If yes please send me the code.....
re: unfolding a bracket
If you have a sheet metal license (which you probably do...), switch to the sheet metal application and use the command (or wizard) "convert to sheet metal" and try to make a flat pattern (or flat solid). If the command can't complete successfully, it may give you information on what needs to change for the unfolding operation to complete.