Get thickness value of solid by selected surface

Get thickness value of solid by selected surface

ngocson8335
Advocate Advocate
884 Views
5 Replies
Message 1 of 6

Get thickness value of solid by selected surface

ngocson8335
Advocate
Advocate

Hello Everyone,

 

I would like to get a thickness of solid by a selected surface on the solid. I would get it like convert a normal part to a sheet metal command.


ThanksNS95_2021-03-11_No08.png

Ngoc Son
Autodesk User
0 Likes
Accepted solutions (1)
885 Views
5 Replies
Replies (5)
Message 2 of 6

aelqabbany
Advocate
Advocate
Accepted solution

Hi,

Try this code. Let me know if you run into an issues.

Sub Main()
	GetSurfaceThickness()
End Sub

Private Sub GetSurfaceThickness()
Dim oFaceA As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select face") 

Dim oSolid As SurfaceBody = oFaceA.Parent

Dim oPartDoc As Document = oSolid.Parent.Document

oUnitVectorA = GetPlaneFaceNormal(oFaceA)

Dim oFaces As Faces = oPartDoc.ComponentDefinition.SurfaceBodies(1).Faces

Dim MinDistance As Double = 9999

For Each oFaceB As Face In oFaces
	oUnitVectorB = GetPlaneFaceNormal(oFaceB)
	If oUnitVectorB.IsParallelTo(oUnitVectorA) Then
		Try
		oDistance = ThisApplication.MeasureTools.GetMinimumDistance(oFaceA, oFaceB)
		Catch
		End Try
		If MinDistance > oDistance And oDistance <> 0
			MinDistance = oDistance
		End If
	End If
Next

MessageBox.Show(MinDistance & " cm", "Surface Thickness in cm")
	
End Sub

Private Function GetPlaneFaceNormal(oFace As Face) As UnitVector 
	Dim Params(0 To 1) As Double

	Dim Normals(0 To 2) As Double

	Params(0) = 0
	Params(1) = 0

	If (TypeOf oFace.Geometry Is Plane) Then
		Call oFace.Evaluator.GetNormal(Params, Normals)
		oUnitNormal = ThisApplication.TransientGeometry.CreateUnitVector(Normals(0), Normals(1), Normals(2))
	End If
	
	GetPlaneFaceNormal = oUnitNormal
End Function
0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor

Here is another way measure the distance from a selected part face to the 'next' part face 'behind' the selected one.

It is a bit longer code, because there are a lot of things to consider and check for, but I think you will like it.  It doesn't just measure to all other parallel faces on the part, it only measures from the selected face to any faces that are 'behind' that face (on the same solid body), then if there was more than one measurement, it returns the measurement to the closest one (smallest non-zero measurement).

It is set up to be used within a Part document, and to measure between planar (flat) part faces only right now, but it could be further customized to include other types of entities and possibly work in other environments (assembly).

It uses the 'FindUsingVector' function, which is defined within the part's component definition.

Here's the iLogic code:

 

Sub Main()
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("A Part Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oPDef As PartComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
	Dim oStartFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a part face.") 
	Dim oStartPt As Point = oStartFace.PointOnFace
	
	'get the face 'normal' ('upwards direction' UnitVector of this face)
	Dim oParams() As Double = {0,0} 'must either specify initial array size, or set values
	Dim oNormals() As Double = {0,0,0} 'must either specify initial array size, or set values
	oStartFace.Evaluator.GetNormal(oParams, oNormals) 'oNormals defines its UnitVector data
	
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

	'create a UnitVector with reverse direction
	'if you are facing a flat part face squarely, it's 'normal' will be pointing back at you
	'we want to search in the opposite direction from that face
	For i As Integer = 0 To 2
		If Not oNormals(i) = 0 Then
			oNormals(i) = oNormals(i) * (-1)
		End If
	Next
	Dim oDir As UnitVector = oTG.CreateUnitVector(oNormals(0), oNormals(1), oNormals(2))
	
	Dim oFilter() As SelectionFilterEnum = {SelectionFilterEnum.kPartFacePlanarFilter} 'SelectionFilterEnum.kAllPlanarEntities
	Dim oObsEnum As ObjectsEnumerator = oPDef.FindUsingVector(oStartPt, oDir, oFilter)
	If oObsEnum.Count <= 1 Then 'because it finds the starting face
		MsgBox("No planar entities found.  Exiting.", , "")
		Exit Sub
	End If
	
	Dim oStartPlane As Plane = oStartFace.Geometry
	Dim oDistances As New List(Of Double)
	Dim oDbl As Double
	For Each oFace As Face In oObsEnum
		If oFace Is oStartFace Then Continue For
		Dim oOtherPlane As Plane = oFace.Geometry
		If oOtherPlane.IsParallelTo(oStartPlane) Then
			oDbl = GetDistance(oStartPlane, oOtherPlane)
			If oDbl > 0 Then
				oDistances.Add(oDbl)
			End If
		End If
	Next
	Dim oDistance As Double
	If oDistances.Count = 0 Then
		MsgBox("No distances in list.  Exiting.", , "")
		Exit Sub
	ElseIf oDistances.Count = 1 Then
		oDistance = oDistances.First
		MsgBox("oDistance = " & oDistance,,"")
	Else
		oDistance = MinOfMany(oDistances.ToArray)
		MsgBox("oDistance = " & oDistance,,"")
'		For Each oDst As Double In oDistances
'			MsgBox("oDst = " & oDst, , "")
'		Next
	End If
End Sub
Public Function GetDistance(ByVal oStart As Plane, ByVal oEnd As Plane) As Double
	Dim oMeasure As MeasureTools = ThisApplication.MeasureTools
	Dim oDist As Double = oMeasure.GetMinimumDistance(oStart, oEnd)
	oDist = ThisApplication.UnitsOfMeasure.ConvertUnits(oDist, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kInchLengthUnits)
	GetDistance = oDist
End Function

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

ngocson8335
Advocate
Advocate

Thanks Mr.

Ngoc Son
Autodesk User
0 Likes
Message 5 of 6

ngocson8335
Advocate
Advocate

I got my point! Thank you so much!

Ngoc Son
Autodesk User
0 Likes
Message 6 of 6

aelqabbany
Advocate
Advocate
You're welcome.
0 Likes