×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Centroids of Faces of a Block

Centroids of Faces of a Block

Centroids of Faces of a Block

(OP)
Dear Forum,

I am trying to get the centroids of the faces that make up a block.

I am using the following code to create the block. Now, I want to get the centroids of the faces.

Dim corner_pt(2) As Double
Dim block_feat_tag As NXOpen.Tag
Dim edge_lengths(2) As String

edge_lengths(0) = "iBarStockLength=" & Length.ToString()
edge_lengths(1) = "iBarStockHeight=" & Height.ToString()
edge_lengths(2) = "iBarStockThickness=" & Thickness.ToString()

Dim sign As FeatureSigns

sign = FeatureSigns.Nullsign

corner_pt(0) = 0
corner_pt(1) = 0
corner_pt(2) = 0


ufs.Modl.CreateBlock1(sign, corner_pt, edge_lengths, block_feat_tag)

If block_feat_tag <> NXOpen.Tag.Null Then
ufs.View.FitView(NXOpen.Tag.Null, 1.0)
'MsgBox("First Solid Body tag is: " & block_feat_tag.ToString)
End If

Dim featName As String = Nothing
theUFSession.Modl.AskFeatName(block_feat_tag, featName)

Dim bodyFeature1 As Features.BodyFeature = CType(workpart.Features.FindObject(featName), Features.BodyFeature)

bodyFeature1.SetName("Solid Copper Bar")

Dim nxBlock As NXObject
nxBlock = CType(bodyFeature1, NXObject)

nxBlock.SetName("Solid Copper Bar")

Return nxBlock

RE: Centroids of Faces of a Block

I was doing something similar a while back (looking for the center point of a rectangular face of a block). Below is the code I ended up using, if you run into problems or make improvements please let me know. In my journal theUfSession was a module level variable.

CODE

Function PlanarFaceMidPoint(ByVal planarFace As Face) As Point3d
 
        Dim uvMinMax(3) As Double
        theUfSession.Modl.AskFaceUvMinmax(planarFace.Tag, uvMinMax)
 
        Dim ptMin(2) As Double
        Dim ptMax(2) As Double
 
        'get mid point of given face
        Dim param() As Double = {(uvMinMax(0) + uvMinMax(1)) / 2, (uvMinMax(2) + uvMinMax(3)) / 2}
        Dim pt(2) As Double
        Dim u1(2) As Double
        Dim v1(2) As Double
        Dim u2(2) As Double
        Dim v2(2) As Double
        Dim unitNormal(2) As Double
        Dim radii(1) As Double
        theUfSession.Modl.AskFaceProps(planarFace.Tag, param, pt, u1, v1, u2, v2, unitNormal, radii)
 
        Dim midPt As New Point3d(pt(0), pt(1), pt(2))
        Return midPt
 
    End Function 

www.nxjournaling.com

RE: Centroids of Faces of a Block

(OP)
Hi Cowski,

Thank you for the code. Except right now I am have problems getting the faces of the Block I've created with the UFUNC functions above.

I've tried the following code:

Dim BarStock As NXObject

BarStock = CreateBarStock(10, 20, 1)

Dim featName As String = Nothing
theUFSession.Modl.AskFeatName(BarStock.Tag, featName)

Dim theBody As Body
theBody = CType(workpart.Bodies.FindObject(featName), Body)

Where the function "CreateBarStock" is my custom function which inside there it is using UFUNC ufs.Modl.CreateBlock1.

How do I get the faces of the block? Don't I need to first cast this NXObject to a body? I've tried that and can't get it to work.

RE: Centroids of Faces of a Block

(OP)
Okay. I got it. I changed my function to return the Features.BodyFeature instance. Then I could simply get the faces directly from the returned object.

dim theFaces() as faces
thefaces = barStock.GetFaces()

As far as the centroid of the faces, I found a one line function within SNAP to get me the centroid of the faces of the block. So, then I cycle through each of the faces and get there centroids.

For Each iFace As Face In theFaces

FaceCenter = Snap.Compute.MassProperties(iFace.GetBody()).Centroid

FaceCenterList.Add(FaceCenter)

Next

Thanks again.

RE: Centroids of Faces of a Block

Your code doesn't do what you think it does, it seems to me. You are getting the mass properties of iFace.GetBody.
But iFace.GetBody is the body on which the face lies, so it's actually the block. In short, you are computing the centroid of the block.

Here are two ways to get face centroids. The first way uses ExtractFace to get a sheet body; the second way uses the mid-UV trick, which will give you the centroid fro block faces, but not for more complicated ones.

CODE --> vb

Option Infer On
Imports Snap, Snap.Create

Class MyProgram

   Public Shared Sub Main()

      Dim myBlock = Block( {0,0,0}, 6, 4, 2 )

      ' Using Extract to get a sheet body (always works)
      For Each face In myBlock.Faces         
         Dim extFeature = ExtractFace(face)
         Dim extBody = extFeature.Body
         Dim props = Snap.Compute.MassProperties(extBody)
         Dim centroid As Position = props.Centroid
      Next

      ' Using the face's UV-box (works only for rectangular faces)
      For Each face In myBlock.Faces         
         Dim box = face.BoxUV
         Dim midU = 0.5*(box.MinU + box.MaxU)
         Dim midV = 0.5*(box.MinV + box.MaxV)
         Dim centerPt As Position = face.Position(midU, midV)
      Next
      
   End Sub

End Class 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources