Selecting faces by color and apply feature thicken on each face separately

Hello,

I would like to select faces by color and apply feature thicken on each face separately.
Can this be done? If yes, what would be the best approach to have the feature put on a single face selection and than move to the next face until all faces are done. Selection of faces might be up to 800 single faces.

Thanks for any input.

Regards,

mlewan

I'd suggest creating a list object to hold the desired faces. You can find the faces by looping through the .Bodies collection of the part, using the .GetFaces method on each body; check the .Color property of each face, if it matches, add it to the list of faces. Finally, loop through the list of faces, creating a thicken feature from each face.

Sounds simple. Do you have a sample code that I can use to test.
Specially for the loop through list of face and creating a feature would be great.

Thanks!

The code below loops through the bodies in the work part and places all the faces of color 6 in a list (color 6 = yellow in the default NX 9 CDF). For example code to create a thicken feature, I suggest recording a journal while creating a thicken feature from a single face.


Option Strict Off

Imports System
Imports System.Collections.Generic
Imports NXOpen

Module Module33
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Dim theFaces As New List(Of Face)

For Each theBody As Body In workPart.Bodies

Dim bodyFaces() As Face
bodyFaces = theBody.GetFaces

For Each tempFace As Face In bodyFaces
'look for yellow faces (yellow = color 6 in the default NX 9 CDF)
If tempFace.Color = 6 Then
theFaces.Add(tempFace)
End If
Next

Next

For Each tempFace As Face In theFaces
'add code to create thicken feature

Next

End Sub

End Module

Great help so far!!!

Thanks,

how to apply now the feature on each face?

here is what I recorded for the feature:

' NX 9.0.3.4
' Journal created by mlewan on Wed Apr 15 12:39:51 2015 Eastern Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Insert->Offset/Scale->Thicken...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_Feature As Features.Feature = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim thickenBuilder1 As Features.ThickenBuilder
thickenBuilder1 = workPart.Features.CreateThickenBuilder(nullFeatures_Feature)

thickenBuilder1.Tolerance = 0.01

thickenBuilder1.FirstOffset.RightHandSide = "15"

thickenBuilder1.SecondOffset.RightHandSide = "-15"

thickenBuilder1.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies1(0) As Body
Dim nullBody As Body = Nothing

targetBodies1(0) = nullBody
thickenBuilder1.BooleanOperation.SetTargetBodies(targetBodies1)

theSession.SetUndoMarkName(markId1, "Thicken Dialog")

thickenBuilder1.RegionToPierce.DistanceTolerance = 0.01

thickenBuilder1.RegionToPierce.ChainingTolerance = 0.0095

Dim faces1(0) As Face
Dim bodyFeature1 As Features.BodyFeature = CType(workPart.Features.FindObject("Tap Hole(1)"), Features.BodyFeature)

Dim face1 As Face = CType(bodyFeature1.FindObject("FACE 300934 {(1797.5,867.4999999999999,665.0000000000002) Tap Hole(1)}"), Face)

faces1(0) = face1
Dim faceDumbRule1 As FaceDumbRule
faceDumbRule1 = workPart.ScRuleFactory.CreateRuleFaceDumb(faces1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = faceDumbRule1
thickenBuilder1.FaceCollector.ReplaceRules(rules1, False)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Thicken")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Thicken")

Dim nXObject1 As NXObject
nXObject1 = thickenBuilder1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Thicken")

Dim expression1 As Expression = thickenBuilder1.SecondOffset

Dim expression2 As Expression = thickenBuilder1.FirstOffset

thickenBuilder1.Destroy()

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

Excellent! You are almost there.

Copy the code you just posted, starting with (and including) the line:

Dim markId1 As Session.UndoMarkId

up to (and including) the line:

thickenBuilder1.Destroy()

Now paste it into the code that I previously posted, right after the comment "'add code to create thicken feature". A few more small tweaks and you will have a working journal.

When you record a journal, any selections you make will be recorded along with the commands used. This will tie a recorded journal to a specific part file. If you try to run your recorded journal on a different part file, you will get an error (or at the very least - unexpected results). This is known as "selection stickiness"; recorded journals must be edited to remove the recorded selection and work with a new selection.

Find the following lines in your new journal and delete them:

Dim bodyFeature1 As Features.BodyFeature = CType(workPart.Features.FindObject("Tap Hole(1)"), Features.BodyFeature)

Dim face1 As Face = CType(bodyFeature1.FindObject("FACE 300934 {(1797.5,867.4999999999999,665.0000000000002) Tap Hole(1)}"), Face)

The "Tap Hole(1)" and "FACE 300934" references are specific to the part file that was active when the journal was recorded. Now we just need to provide our face object in place of the recorded one.

Find the line:

faces1(0) = face1

and edit it to be:

faces1(0) = tempFace

Save your new journal and try it out. It should create a thicken feature from every yellow face in the work part (assuming the thicken feature can be created from the input).

Wow,

that works, thanks a lot!
You just mad my day.

Wow,

that works, thanks a lot!
You just mad my day.

Some of the faces can not be offset. The feature fails. Is it possible to continue and display a message at the end that not all surfaces could be offset?

Thanks

We'll need to wrap the code that creates the thicken feature in a Try block. If an error occurs creating a thicken feature, it will skip it and move to the next face. The only code that needs to be in the Try block is the call to the thickenBuilder.Commit method. If an error occurs with the thicken feature, this is the line of code that will throw the error.

The Try block will look something like this:

For Each tempFace As Face In theFaces
'code setting up the thicken feature builder properties

Try
nXObject1 = thickenBuilder1.Commit()

Catch ex As NXException
lw.WriteLine(ex.Message)
theSession.DeleteUndoMark(markId1, "Thicken")

Finally
thickenBuilder1.Destroy()

End Try

Next

The names of your variables and/or undo mark might be slightly different, these won't matter as long as you put the call to .Commit in the Try block. We put the call to the .Destroy method in the Finally part of the block because we want that code to run whether there was an error or not (the builder object was created, now we must clean it up).