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!

Centroids of Faces of a Block

Status
Not open for further replies.

EngProgrammer

Aerospace
Jan 14, 2015
150
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
 
Replies continue below

Recommended for you

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
 
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.
 
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.

 
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:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor