NX VB Script to Move Selected Body to ACS

2024-08-22T11:52:48Z
NX Open NX for Manufacturing

Summary


Details

How to position model to Absolut Coordinate System?

Solution

Menu->Edit->Move Object

Or execute this VB Script:

' NX 2306.3001 ' Journal created by silva on Fri Apr 5 10:49:44 2024 Hora oficial do Brasil Option Strict Off Imports System Imports System.Collections Imports NXOpen Imports NXOpen.UF Imports NXOpen.UI Imports NXOpen.Features Imports NXOpen.Utilities Imports NXOpen.Selection Module NXJournal Dim theSession As NXOpen.Session = NXOpen.Session.GetSession() Dim ufs As UFSession = UFSession.GetUFSession() Dim theUI As UI = UI.GetUI() Dim workPart As NXOpen.Part = theSession.Parts.Work Dim displayPart As NXOpen.Part = theSession.Parts.Display Dim lw As ListingWindow = theSession.ListingWindow() <Obsolete> Sub Main(ByVal args() As String) Dim measurePrefsBuilder1 As NXOpen.MeasurePrefsBuilder = Nothing Dim scCollector1 As NXOpen.ScCollector = Nothing Dim selectionIntentRuleOptions1 As NXOpen.SelectionIntentRuleOptions = Nothing theSession.CAMSession.SetMcsDisplay(False) workPart.WCS.Visibility = True measurePrefsBuilder1 = theSession.Preferences.CreateMeasurePrefsBuilder() measurePrefsBuilder1.InfoUnits = NXOpen.MeasurePrefsBuilder.JaMeasurePrefsInfoUnit.CustomUnit measurePrefsBuilder1.ConsoleOutput = False measurePrefsBuilder1.ShowValueOnlyToggle = False measurePrefsBuilder1.ConsoleOutput = False scCollector1 = workPart.ScCollectors.CreateCollector() scCollector1.SetMultiComponent() selectionIntentRuleOptions1 = workPart.ScRuleFactory.CreateRuleOptions() selectionIntentRuleOptions1.SetSelectedFromInactive(False) 'Ask user to select object Dim body1 As Body = SelectBody() Dim sellist1(0) As NXOpen.NXObject If body1 IsNot Nothing Then sellist1(0) = body1 End If Dim bodyDumbRule1 As NXOpen.BodyDumbRule = Nothing Dim rules1(0) As NXOpen.SelectionIntentRule selectionIntentRuleOptions1.Dispose() rules1(0) = bodyDumbRule1 scCollector1.ReplaceRules(rules1, False) Dim ptanchor1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0) Dim boxpoints1() As NXOpen.Point3d Dim dir1() As NXOpen.Point3d Dim edgelength1() As Double Dim ptorigin1 As NXOpen.Point3d = Nothing Dim ptextreme1 As NXOpen.Point3d = Nothing Dim pdvolume1 As Double = Nothing theSession.Measurement.GetBoundingBoxProperties(sellist1, 1, ptanchor1, False, boxpoints1, dir1, edgelength1, ptorigin1, ptextreme1, pdvolume1) measurePrefsBuilder1.ConsoleOutput = False Dim xvec As Vector3d = New Vector3d(dir1(0).X, dir1(0).Y, dir1(0).Z) Dim yvec As Vector3d = New Vector3d(dir1(1).X, dir1(1).Y, dir1(1).Z) Dim zvec As Vector3d = New Vector3d(dir1(2).X, dir1(2).Y, dir1(2).Z) Dim cornerCsys As CartesianCoordinateSystem Dim fromCsys As CartesianCoordinateSystem cornerCsys = workPart.CoordinateSystems.CreateCoordinateSystem(ptorigin1, xvec, yvec) workPart.WCS.SetCoordinateSystemCartesianAtCsys(cornerCsys) Dim fromdapnt() As Double = ConvertPoint3dToDoubleArray(ptorigin1) Dim fromwcspnt As NXOpen.Point3d = New NXOpen.Point3d(edgelength1(0) / 2, edgelength1(1) / 2, edgelength1(2)) Dim fromabspnt As NXOpen.Point3d = WCS2Abs(fromwcspnt) fromCsys = workPart.CoordinateSystems.CreateCoordinateSystem(fromabspnt, xvec, yvec) workPart.WCS.SetCoordinateSystemCartesianAtCsys(fromCsys) Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing Dim nullNXOpen_Body As NXOpen.Body = Nothing Dim blockFeatureBuilder1 As NXOpen.Features.BlockFeatureBuilder = Nothing Dim feature1 As NXOpen.Features.Feature = Nothing blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullNXOpen_Features_Feature) blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.DiagonalPoints blockFeatureBuilder1.SetTwoDiagonalPoints(ptorigin1, ptextreme1) blockFeatureBuilder1.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, nullNXOpen_Body) feature1 = blockFeatureBuilder1.CommitFeature() blockFeatureBuilder1.Destroy() scCollector1.Destroy() Dim FrmCsys = workPart.WCS.Save() FrmCsys.Layer = 2 Dim wcsOrigin As Point3d = FrmCsys.Origin Dim absOrigin As Point3d = New Point3d(0, 0, 0) Dim absMtx As Matrix3x3 = New Matrix3x3 With { .Xx = 1, .Xy = 0, .Xz = 0, .Yx = 0, .Yy = 1, .Yz = 0, .Zx = 0, .Zy = 0, .Zz = 1 } Dim absCsys As CartesianCoordinateSystem absCsys = workPart.CoordinateSystems.CreateCoordinateSystem(absOrigin, absMtx, False) absCsys.Layer = 2 Dim bodies As BodyCollection = workPart.Bodies Dim k As Integer = 0 Dim movObjs(-1) As NXObject For Each oneBody As Body In bodies ReDim Preserve movObjs(k) Dim movBodylst As NXObject = CType(oneBody, NXObject) movObjs(k) = NXOpen.Utilities.NXObjectManager.Get(movBodylst.Tag) k += 1 Next If Not Point3dAreEqual(absOrigin, wcsOrigin) Then MoveObjectByCsysToCsys(movObjs, FrmCsys, absCsys) End If workPart.WCS.SetOriginAndMatrix(absOrigin, absMtx) ufs.Obj.DeleteObject(cornerCsys.Tag) ufs.Obj.DeleteObject(fromCsys.Tag) ufs.Obj.DeleteObject(FrmCsys.Tag) ufs.Obj.DeleteObject(absCsys.Tag) ufs.Obj.DeleteObject(feature1.Tag) workPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit) workPart.MeasureManager.ClearPartTransientModification() workPart.MeasureManager.SetPartTransientModification() workPart.MeasureManager.ClearPartTransientModification() End Sub Function SelectBody() As Body Dim body1 As Body = Nothing ' Get UI object Dim theUI As NXOpen.UI = NXOpen.UI.GetUI() Dim sel As NXOpen.Selection = theUI.SelectionManager Dim objects1(0) As NXOpen.DisplayableObject Dim selObj As TaggedObject = Nothing Dim cursor As Point3d Dim message As String = "Select Body" Dim title As String = "Selection" Dim resp As Response = sel.SelectTaggedObject(message, title, SelectionScope.UseDefault, False, False, selObj, cursor) If resp <> Response.Back And resp <> Response.Cancel Then If TypeOf selObj Is Body Then body1 = CType(selObj, Body) End If End If Return body1 End Function Function ConvertPoint3dToDoubleArray(ByVal pt As Point3d) As Double() Dim pt3d2da As Double() = {pt.X, pt.Y, pt.Z} Return pt3d2da End Function Function WCS2Abs(ByVal inPt As Point3d) As Point3d Dim pt1(2), pt2(2) As Double pt1(0) = inPt.X pt1(1) = inPt.Y pt1(2) = inPt.Z ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt1, UFConstants.UF_CSYS_ROOT_COORDS, pt2) WCS2Abs.X = pt2(0) WCS2Abs.Y = pt2(1) WCS2Abs.Z = pt2(2) End Function Function SelectAllObjectsLayer(lyrnum As Integer) Dim objects() As NXObject = workPart.Layers.GetAllObjectsOnLayer(lyrnum) Dim objList As ArrayList = New ArrayList() For Each oneObject As DisplayableObject In objects objList.Add(oneObject) Next Return objList.ToArray(GetType(NXObject)) End Function Sub MoveObjectByCsysToCsys(ByRef nXObject() As NXObject, ByVal nXObjectCsysFrom As CartesianCoordinateSystem, ByVal nXObjectCsysTo As CartesianCoordinateSystem) Try Dim nullNXOpen_Features_MoveObject As NXOpen.Features.MoveObject = Nothing Dim moveObjectBuilder As NXOpen.Features.MoveObjectBuilder = theSession.Parts.Work.BaseFeatures.CreateMoveObjectBuilder(nullNXOpen_Features_MoveObject) moveObjectBuilder.TransformMotion.Option = NXOpen.GeometricUtilities.ModlMotion.Options.CsysToCsys moveObjectBuilder.MoveParents = True moveObjectBuilder.ObjectToMoveObject.Add(nXObject) moveObjectBuilder.TransformMotion.Option = GeometricUtilities.ModlMotion.Options.CsysToCsys moveObjectBuilder.TransformMotion.FromCsys = nXObjectCsysFrom moveObjectBuilder.TransformMotion.ToCsys = nXObjectCsysTo moveObjectBuilder.Commit() Dim objects() As NXObject = moveObjectBuilder.GetCommittedObjects() moveObjectBuilder.Destroy() Catch nXException As NXException UI.GetUI().NXMessageBox.Show("Move Object", NXOpen.NXMessageBox.DialogType.Error, "Unable to Move object. Error : " + nXException.Message.ToString()) End Try End Sub <Obsolete> Function Point3dAreEqual(oneP As Point3d, twoP As Point3d) As Boolean Dim oneLoc() As Double = {oneP.X, oneP.Y, oneP.Z} Dim twoLoc() As Double = {twoP.X, twoP.Y, twoP.Z} Dim tol As Double Dim match As Integer ufs.Modl.AskDistanceTolerance(tol) ufs.Vec3.IsEqual(oneLoc, twoLoc, tol, match) If (match = 0) Then Return False Else Return True End If End Function End Module

KB Article ID# PL8792656

Contents

SummaryDetails

Associated Components

NX Open - All Languages