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

Hole on Datum in reference to WCS

$
0
0

I want to create a Journal that will create holes in reference to WCS. We wan't to have a button which (after placing the WCS on a face ; Z axis perpendicular to plane) will create hole at X,Y coordinates in reference to WCS.

Since AFAIK the only option in UG to position a hole is a sketch or datum, my idea is to create a Datum in WCS, reposition it to the new coords and create a Hole attached to this datum.

UPDATE:

I came up with code that creates Datums and Holes and looks like it works but the problem is that the Datums are broken. Origin is detached from Datum and after modification of one Datum all Datum CSYS are goint to the same point.

Can someone help me with that? I think the problem lies in the fixed reference to "POINT 1" CType(datumCsys2.FindObject("POINT 1")
I wanted to use CType(datumCsys2.Location) but it's a Point3D

I dont really know what effect has the:
rule1
helpPoint1

in .HolePosition.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)

"working" code

' NX 10.0.1.4' Journal created by wizdar on Mon Mar 21 12:52:39 2016 Mitteleuropäische Zeit'Option Strict OffImports System
Imports NXOpen
Imports NXOpen.UF 
 
 
Module NXJournal
	Dim theSession As NXOpen.Session= NXOpen.Session.GetSession()Dim workPart As NXOpen.Part= theSession.Parts.WorkDim ufs As UFSession = UFSession.GetUFSession()Dim displayPart As NXOpen.Part= theSession.Parts.Display 
 
	Sub Main (ByVal args()AsString) 
		Dim markId1 As NXOpen.Session.UndoMarkId
		markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start") 
		'How to make body select??Dim body1 As NXOpen.Body
		body1 = SelectSolid() 
		Dim itargetBodies(0)As NXOpen.Body
		itargetBodies(0)= body1
 
		IfNot workPart.Preferences.Modeling.GetHistoryModeThenThrow(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))EndIf 
		CreateHole(0, 0, itargetBodies)
		CreateHole(15, 0, itargetBodies)
		CreateHole(30, 0, itargetBodies) 
	EndSub 
	Sub CreateHole (xcoord AsDouble, ycoord AsDouble, targetBodies As NXOpen.Body()) 
		Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature=Nothing 
		Dim datumCsysBuilder1 As NXOpen.Features.DatumCsysBuilder
		datumCsysBuilder1 = workPart.Features.CreateDatumCsysBuilder(nullNXOpen_Features_Feature) 
		Dim cartesianCoordinateSystem1 As NXOpen.CartesianCoordinateSystem
		cartesianCoordinateSystem1 = workPart.WCS.CoordinateSystem 
		'Define origin 
		Dim orPt As Point3d
			 orPt.X= xcoord
			 orPt.Y= ycoord
			 orPt.Z=0'New origin for CSYS
		cartesianCoordinateSystem1.Origin= WCS2Abs(orPt) 
		datumCsysBuilder1.Csys= cartesianCoordinateSystem1
 
		datumCsysBuilder1.DisplayScaleFactor=0.5 
		Dim nXObject1 As NXOpen.NXObject
		nXObject1 = datumCsysBuilder1.Commit() 
		Dim datumCsys2 As NXOpen.Features.DatumCsys=CType(nXObject1, NXOpen.Features.DatumCsys) 
		'Need to select datum origin. 
		Dim point1 As NXOpen.Point=CType(datumCsys2.FindObject("POINT 1"), NXOpen.Point) 
		'Dim point1 As NXOpen.Point = CType(datumCsys2.Location, NXOpen.Point) 
		datumCsysBuilder1.Destroy() 
		'Create Hole PartDim nullNXOpen_Features_HolePackage As NXOpen.Features.HolePackage=Nothing 
		Dim holePackageBuilder1 As NXOpen.Features.HolePackageBuilder
		holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullNXOpen_Features_HolePackage) 
		'Point 
		Dim points1(0)As NXOpen.Point
		points1(0)= point1
		Dim curveDumbRule1 As NXOpen.CurveDumbRule
		curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1) 
 
		Dim rules1(0)As NXOpen.SelectionIntentRule
		rules1(0)= curveDumbRule1
		Dim nullNXOpen_NXObject As NXOpen.NXObject=Nothing 
		Dim helpPoint1 As NXOpen.Point3d=New NXOpen.Point3d(0.0, 0.0, 0.0) 
 
		With holePackageBuilder1
 
			.HolePosition.AllowSelfIntersection(True)
			.HolePosition.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)
			.BooleanOperation.Type= NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract
			.BooleanOperation.SetTargetBodies(targetBodies) 
		EndWith 
		'Hole Type 6With holePackageBuilder1
 
			.GeneralHoleForm= Features.HolePackageBuilder.HoleForms.Simple
			.HoleDepthLimitOption= Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody
			.GeneralSimpleHoleDiameter.RightHandSide="6"
			.GeneralSimpleHoleDepth.RightHandSide="12"
			.Tolerance=0.001 
		EndWith 
		'Commit HoleDim nXObject2 As NXOpen.NXObject
		nXObject2 = holePackageBuilder1.Commit() 
		holePackageBuilder1.Destroy() 
 
	EndSub 
 
 
 
	Function SelectSolid()As Body
 
		Dim ui As UI = ui.GetUIDim message AsString="Select solid body"Dim title AsString="Selection" 
		Dim scope As Selection.SelectionScope= Selection.SelectionScope.WorkPartDim keepHighlighted AsBoolean=FalseDim includeFeatures AsBoolean=True 
		Dim selectionAction As Selection.SelectionAction= _
		   Selection.SelectionAction.ClearAndEnableSpecific 
		Dim selectionMask_array(0)As Selection.MaskTripleWith selectionMask_array(0)
		   .Type= UFConstants.UF_solid_type
		   .Subtype=0
		   .SolidBodySubtype= UFConstants.UF_UI_SEL_FEATURE_SOLID_BODYEndWith 
		Dim selectedObject As NXObject =NothingDim cursor As Point3d
 
		ui.SelectionManager.SelectObject(message, title, scope, _
										selectionAction, includeFeatures, _
										keepHighlighted, selectionMask_array, _
										selectedObject, cursor) 
		Dim solid As Body =CType(selectedObject, Body) 
		If solid IsNothingThenReturnNothingEndIf 
		Return solid
 
	EndFunction 
 
	Function Abs2WCS(ByVal inPt As Point3d)As Point3d
		Dim pt1(2), pt2(2)AsDouble 
		pt1(0)= inPt.X
		pt1(1)= inPt.Y
		pt1(2)= inPt.Z 
		ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_COORDS, pt1, _
			UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt2) 
		Abs2WCS.X= pt2(0)
		Abs2WCS.Y= pt2(1)
		Abs2WCS.Z= pt2(2) 
	EndFunction 
	Function WCS2Abs(ByVal inPt As Point3d)As Point3d
		Dim pt1(2), pt2(2)AsDouble 
		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) 
	EndFunction 
 
    PublicFunction GetUnloadOption(ByVal dummy AsString)AsInteger 
        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination 
    EndFunction 
 
End Module

Viewing all articles
Browse latest Browse all 783

Trending Articles



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