Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

journal to move sketches on layer and rest curves on another layer

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
Hello,

I'm writing some journal to move some part objects to specifics layers. For example planes on 60 sketches on 70 and rest of curves on 80. Right now I have some problems with curves if they are in sketch, because my code move them all to layer 80.

Code:
for each curveObj as Curve in WorkPart.Curves  
	curveObj.Layer = CurveLayer  
	curveObj.RedisplayObject  
next 

For Each tempSketch As Sketch In workPart.Sketches
	tempSketch.layer = SketchLayer
	tempSketch.RedisplayObject
next
How can I exclude curves which are parts of sketch? I tried:

Code:
curveObj.OwningComponent.tostring = "sketch"

but an error occurred with info that "system.nullreferenceexception object reference not set to an instance of an object".


With best regards
Michael
 
Replies continue below

Recommended for you

There's more than one way to approach this issue, but I'd probably iterate through the curves collection and use the .AskObjectFeat method to see what feature (if any) owns the curve. If a sketch feature owns the curve, move it to the sketch layer, otherwise move it to the curve layer.

www.nxjournaling.com
 
Thanks @Cowski for tip. I use some part of Your code from NXjournaling:

Code:
for each myCurve as Curve in WorkPart.Curves  

	Dim featTag As Tag = Tag.Null
	Dim myFeature As Features.Feature
 
	ufs.Modl.AskObjectFeat(myCurve.Tag, featTag)
	
	If featTag = Tag.Null Then
		
		myCurve.Layer = CurveLayer  
		myCurve.RedisplayObject  
			
	Else
		
		myFeature = Utilities.NXObjectManager.Get(featTag)
		dim FeatName as string = myFeature.GetFeatureName
		
		if FeatName.contains("SKETCH") then 
			
			myCurve.Layer = SketchLayer  
			myCurve.RedisplayObject  
	
		else 
	
			myCurve.Layer = CurveLayer  
			myCurve.RedisplayObject  
		
		end if
			
	End If
	
next
I tried to do similar on point, but all my points are unused, even if one of them is in sketch. Any tip?

With best regards
Michael
 
I would also create a separate file (XML, TXT or CSV) for the layer definitions. In this definition file you can state which features need to go to which layer.
If something changes in the future you can easily adapt the layers in the file with no need to dive into the source code and recompile.

Ronald van den Broek
Senior Application Engineer
Winterthur Gas & Diesel Ltd
NX9 / TC10.1.2

Building new PLM environment from Scratch using NX12 / TC11
 
The easiest way I found was to move the sketches first & then the curves.

Basicaly it works like this:
1. Show all Layers.
2. Find & move all sketches to layer X.
3. Hide layer X.
4. Find & move all curves to layer Y.
5. Hide layer Y.

"It is impossible to make anything foolproof because fools are so ingenious."
 
niedzviedz said:
I tried to do similar on point, but all my points are unused, even if one of them is in sketch. Any tip?

In my testing, points created in the sketch with the "point" command were reported correctly by .AskObjectFeat. However, if the point was a result of an "intersection point", it was reported as used by an intersection point feature that was internal to the sketch feature. It is possible to check for this condition in your code, but it adds to the complexity.

I found that the sketch's .GetAllGeometry method will give you direct access to normal points and intersection points. So, a potentially easier alternative would be to iterate through the sketches, get the geometry from each, and do with it what you want.

www.nxjournaling.com
 
@Cowski Try my test model. There are 4 points:
1) point from point command
2) point from point command plus removed parameters
3) point from sketch
4) point from datum_CSYS included in sketch.

Each of them give the answer - point is unused. I tested it on NX 10.


With best regards
Michael
 
 https://files.engineering.com/getfile.aspx?folder=1f2681c5-3695-42b8-aaba-8f201081d4d7&file=test.prt
The point in the sketch is correctly reported for me (NX 10.0.3.5 MP12). Below is my test code:
Edit: All the point objects are correctly reported, not just the one in the sketch.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module4
    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()
        lw.Open()
        Dim workPart As Part = theSession.Parts.Work

        Dim featTag As Tag = Tag.Null

        For Each myCurve As Curve In workPart.Curves
            lw.WriteLine("curve tag: " & myCurve.Tag.ToString)
            theUfSession.Modl.AskObjectFeat(myCurve.Tag, featTag)
            If featTag = Tag.Null Then
                'curve is unused
                lw.WriteLine("unused")
            Else
                Dim theFeat As Features.Feature = Utilities.NXObjectManager.Get(featTag)
                lw.WriteLine("used by: " & theFeat.GetFeatureName)
            End If
            lw.WriteLine("")
        Next

        For Each myPt As Point In workPart.Points
            lw.WriteLine("point tag: " & myPt.Tag.ToString)
            theUfSession.Modl.AskObjectFeat(myPt.Tag, featTag)
            If featTag = Tag.Null Then
                'curve is unused
                lw.WriteLine("unused")
            Else
                Dim theFeat As Features.Feature = Utilities.NXObjectManager.Get(featTag)
                lw.WriteLine("used by: " & theFeat.GetFeatureName)
            End If
            lw.WriteLine("")
        Next

        For Each tempSketch As Sketch In workPart.Sketches
            lw.WriteLine(tempSketch.Name)
            Dim sketchGeo() As NXObject = tempSketch.GetAllGeometry
            Dim curveCount As Integer = 0
            Dim ptCount As Integer = 0
            For Each geo As NXObject In sketchGeo
                If TypeOf (geo) Is Curve Then
                    curveCount += 1
                ElseIf TypeOf (geo) Is Point Then
                    ptCount += 1
                End If
            Next
            lw.WriteLine("  contains " & curveCount.ToString & " curves")
            lw.WriteLine("  contains " & ptCount.ToString & " points")
            lw.WriteLine("")
        Next

        lw.Close()
    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Yes. your code works ok, but my not. I wonder why, they are close the same. I have NX10 MR3 MP19.

Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.Utilities
Imports NXOpen.UF

Module NXJournal

Sub Main (ByVal args() As String) 

	Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
	Dim ufs As UFSession = UFSession.GetUFSession()
	Dim workPart As NXOpen.Part = theSession.Parts.Work
	Dim displayPart As NXOpen.Part = theSession.Parts.Display

	Dim lw As ListingWindow = theSession.ListingWindow

	lw.open 

	Dim DatumLayer As Integer = 60
	Dim CsysLayer as Integer = 70
	Dim SketchLayer as Integer = 70
	Dim CurveLayer as Integer = 80
	Dim PointLayer as Integer = 80


	for each datumObj as DisplayableObject in workPart.Datums  

		datumObj.Layer = DatumLayer  
		datumObj.RedisplayObject  
	Next 
	
	for each MyPoint as Point in WorkPart.Points  
	
		Dim featTag As Tag = Tag.Null
		Dim myFeature As Features.Feature
		
		If featTag = Tag.Null Then
		
		'point is unused
		lw.WriteLine("point is unused")
		'MyPoint.Layer = CurveLayer  
		'MyPoint.RedisplayObject  
			
		Else
	
			myFeature = Utilities.NXObjectManager.Get(featTag)
			lw.WriteLine("used by: " & myFeature.GetFeatureName)
			
     		End If
		
	next 
	
	For Each myFeature As Feature In workPart.Features
 
		If TypeOf (myFeature) Is DatumCsys Then
 
			'uncomment the following If block to skip internal features
			If myFeature.IsInternal Then
			    Continue For
			End If
 
			Dim csys_tag As Tag
			Dim origin_tag As Tag
			Dim daxes As Tag()
			Dim dplanes As Tag()
			ufs.Modl.AskDatumCsysComponents(myFeature.Tag, csys_tag, origin_tag, daxes, dplanes)
			ufs.Obj.SetLayer(origin_tag, CsysLayer)
			ufs.Obj.SetLayer(csys_tag,CsysLayer)
 
			For Each thisObj As NXOpen.Tag In daxes	
			
				ufs.Obj.SetLayer(thisObj, CsysLayer)
				
			Next
 
			For Each thisObj As NXOpen.Tag In dplanes
		
				ufs.Obj.SetLayer(thisObj, CsysLayer)
			
			Next
				
		End If
 
	Next

	for each myCurve as Curve in WorkPart.Curves  

		Dim featTag As Tag = Tag.Null
		Dim myFeature As Features.Feature
 
		ufs.Modl.AskObjectFeat(myCurve.Tag, featTag)
		
		If featTag = Tag.Null Then
		
			'curve is unused
			'lw.WriteLine("curve is unused")
			myCurve.Layer = CurveLayer  
			myCurve.RedisplayObject  
			
		Else
		
			myFeature = Utilities.NXObjectManager.Get(featTag)
			'lw.WriteLine("used by: " & myFeature.GetFeatureName)
			dim FeatName as string = myFeature.GetFeatureName
		
			'lw.writeline(fname)
		
			if FeatName.contains("SKETCH") then 
			
				myCurve.Layer = SketchLayer  
				myCurve.RedisplayObject  
	
			else 
	
				myCurve.Layer = CurveLayer  
				myCurve.RedisplayObject  
		
			end if
			
		End If
	
	next 


	Dim stateArray1(0) As NXOpen.Layer.StateInfo
	stateArray1(0) = New NXOpen.Layer.StateInfo(60, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray1, False)

	Dim stateArray2(0) As NXOpen.Layer.StateInfo
	stateArray2(0) = New NXOpen.Layer.StateInfo(70, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray2, False)

	Dim stateArray3(0) As NXOpen.Layer.StateInfo
	stateArray3(0) = New NXOpen.Layer.StateInfo(80, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray3, False)

	lw.close

End Sub

End Module

With best regards
Michael
 
 https://files.engineering.com/getfile.aspx?folder=1f2681c5-3695-42b8-aaba-8f201081d4d7&file=test.prt
Now I see, I don't have theUfSession.Modl.AskObjectFeat(myPt.Tag, featTag) in my loop, that's why I receive info - point is unused.
@Cowski Thanks for Your help.
Last question - now I have two loops to process datum objects, first for all and second for datum_CSYS. I would like to reduce them to one, but if I uncomment:

Code:
ElseIf TypeOf (myFeature) Is Datumplane Then

An error appears.

With best regards
Michael
 
I place it after If TypeOf (myFeature) Is DatumCsys Then.

An error is: the type of expression "NXopen.features.feature" never can be type of "NXopen.datumplane". Sorry for grammar errors, I translate it from my language.

With best regards
Michael
 
In the NXOpen API there is a NXOpen.DatumPlane object (the actual datum plane object) and the NXOpen.Features.DatumPlaneFeature (the feature that creates/maintains the datum plane object). It appears that you are mixing them up.

www.nxjournaling.com
 
Thanks @Cowski. I'm looking in documentation for NXOpen.Features.DatumPlaneFeature for layer and can't find anything. Any clues?


With best regards
Michael
 
Features don't have layer properties, only the actual geometric objects do. If you have a reference to a datum plane feature, you can access the datum plane object through the .DatumPlane property. If you want to move the datum plane to a new layer it might look like this:

Code:
myDatumPlaneFeature.DatumPlane.Layer = 123
myDatumPlaneFeature.DatumPlane.RedisplayObject

www.nxjournaling.com
 
Thank for tip, I made so tests, and error appears:
"system.nullreferenceexception object reference not set to an instance of an object"
Here is my code:

Code:
ElseIf TypeOf (myFeature) Is DatumPlaneFeature Then
	Dim myDatumPlaneFeature as Datumplane 
	'myDatumPlaneFeature = myFeature.datumplane
	myDatumPlaneFeature.Layer = 123
	myDatumPlaneFeature.RedisplayObject
	'myFeature.DatumPlane.Layer = 123
End If

I tried to set My datumplaneFeature to myFeature, but it didn't worked.

With best regards
Michael
 
Perhaps something like below (untested):

Code:
ElseIf TypeOf (myFeature) Is DatumPlaneFeature Then
    Dim myDatumPlaneFeature as Features.DatumPlaneFeature = myFeature
    myDatumPlaneFeature.DatumPlane.Layer = 123
    myDatumPlaneFeature.DatumPlane.RedisplayObject
End If

www.nxjournaling.com
 
Thank @Cowski for help. I missed one very important word -> Features. Without it I received an error kind "Element DatumplaneFeature is not a member of NXopen.Features.feature".
Below is my latest code. I discovered that if point is part of datum_csys which is part of sketch, it will be moved to layer, so I must add some exception for it.

Code:
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.Utilities
Imports NXOpen.UF

Module NXJournal

Sub Main (ByVal args() As String) 

	Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
	Dim ufs As UFSession = UFSession.GetUFSession()
	Dim workPart As NXOpen.Part = theSession.Parts.Work
	Dim displayPart As NXOpen.Part = theSession.Parts.Display

	Dim lw As ListingWindow = theSession.ListingWindow

	lw.open 

	Dim DatumLayer As Integer = 60
	Dim CsysLayer as Integer = 65
	Dim SketchLayer as Integer = 70
	Dim CurveLayer as Integer = 75
	Dim PointLayer as Integer = 80


	for each MyPoint as Point in WorkPart.Points  
	
		Dim featTag As Tag = Tag.Null
		Dim myFeature As Features.Feature
		ufs.Modl.AskObjectFeat(myPoint.Tag, featTag)

		If featTag = Tag.Null Then
		
			'point is unused
			'lw.WriteLine("point is unused")

			MyPoint.Layer = PointLayer  
			MyPoint.RedisplayObject  
			
		Else	
		
			myFeature = Utilities.NXObjectManager.Get(featTag)
			'lw.WriteLine("used by: " & myFeature.GetFeatureName)
			dim FeatName as string = myFeature.GetFeatureName

			if FeatName.contains("SKETCH") then 
			
				myPoint.Layer = SketchLayer  
				myPoint.RedisplayObject  
	
			elseif FeatName.contains("DATUM_CSYS") then 
			
				myPoint.Layer = CsysLayer
				myPoint.RedisplayObject  

			else 		

				myPoint.Layer = PointLayer  
				myPoint.RedisplayObject  
		
			end if


	 		End If
		
	next 
	
	For Each myFeature As Feature In workPart.Features
 
		If TypeOf (myFeature) Is DatumCsys Then
 
			'uncomment the following If block to skip internal features
			If myFeature.IsInternal Then
				'Continue For
			End If
 
			Dim csys_tag As Tag
			Dim origin_tag As Tag
			Dim daxes As Tag()
			Dim dplanes As Tag()
			ufs.Modl.AskDatumCsysComponents(myFeature.Tag, csys_tag, origin_tag, daxes, dplanes)
			ufs.Obj.SetLayer(origin_tag, CsysLayer)
			ufs.Obj.SetLayer(csys_tag, CsysLayer)
 
			For Each thisObj As NXOpen.Tag In daxes	
			
				ufs.Obj.SetLayer(thisObj, CsysLayer)
				
			Next
 
			For Each thisObj As NXOpen.Tag In dplanes
		
				ufs.Obj.SetLayer(thisObj, CsysLayer)
			
			Next

		ElseIf TypeOf (myFeature) Is DatumPlaneFeature Then

			Dim myDatum as Features.DatumPlaneFeature = myFeature
			myDatum.DatumPlane.Layer = DatumLayer
			myDatum.DatumPlane.RedisplayObject

		End If
 
	Next

	for each myCurve as Curve in WorkPart.Curves  

		Dim featTag As Tag = Tag.Null
		Dim myFeature As Features.Feature
 
		ufs.Modl.AskObjectFeat(myCurve.Tag, featTag)
		
		If featTag = Tag.Null Then
		
			'curve is unused
			'lw.WriteLine("curve is unused")
			myCurve.Layer = CurveLayer  
			myCurve.RedisplayObject  
			
		Else
		
			myFeature = Utilities.NXObjectManager.Get(featTag)
			'lw.WriteLine("used by: " & myFeature.GetFeatureName)
			dim FeatName as string = myFeature.GetFeatureName
		
			'lw.writeline(featName)
		
			if FeatName.contains("SKETCH") then 
			
				myCurve.Layer = SketchLayer  
				myCurve.RedisplayObject  
	
			else 
	
				myCurve.Layer = CurveLayer  
				myCurve.RedisplayObject  
		
			end if
			
		End If
	
	next 

	For Each MySketch As Sketch In workPart.Sketches
	
		'lw.WriteLine(mySketch.Name)
		mySketch.Layer = SketchLayer
		mySketch.RedisplayObject
			
	Next

	Dim stateArray1(0) As NXOpen.Layer.StateInfo
	stateArray1(0) = New NXOpen.Layer.StateInfo(60, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray1, False)

	Dim stateArray2(0) As NXOpen.Layer.StateInfo
	stateArray2(0) = New NXOpen.Layer.StateInfo(65, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray2, False)
	
	Dim stateArray3(0) As NXOpen.Layer.StateInfo
	stateArray3(0) = New NXOpen.Layer.StateInfo(70, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray3, False)
	
	Dim stateArray4(0) As NXOpen.Layer.StateInfo
	stateArray4(0) = New NXOpen.Layer.StateInfo(75, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray4, False)
	
	Dim stateArray5(0) As NXOpen.Layer.StateInfo
	stateArray5(0) = New NXOpen.Layer.StateInfo(80, NXOpen.Layer.State.Hidden)
	workPart.Layers.ChangeStates(stateArray5, False)

	lw.close

End Sub

End Module

With best regards
Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor