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
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
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 Functionwww.nxjournaling.com
RE: Centroids of Faces of a Block
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
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
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