- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm using a modified VBA code from Mod the Machine to find the thickness of all of my platework. This is the code:
Function GetThickness(sb As SurfaceBody) As Double
' Find biggest face
Dim f As Face
Dim bf As Face
Dim area As Double
For Each f In sb.Faces
' Only care about planar faces
If TypeOf f.Geometry Is Plane And f.Evaluator.area > area Then
Set bf = f
area = f.Evaluator.area
End If
Next
' Find the opposite face
Dim p As Plane
Set p = bf.Geometry
Dim pt1 As Point
Set pt1 = bf.PointOnFace
Dim tr As TransientGeometry
Set tr = ThisApplication.TransientGeometry
Dim objs As ObjectsEnumerator
Dim pts As ObjectsEnumerator
Dim n As UnitVector
' We have to search in the opposite direction
' of the face's normal vector
If bf.IsParamReversed Then
Set n = p.Normal
Else
Set n = tr.CreateUnitVector( _
-p.Normal.X, -p.Normal.Y, -p.Normal.Z)
End If
' objs(2) should be the opposite face
' but we do not need it, the intersection point
' is enough, i.e. pts(2)
Call sb.FindUsingRay(pt1, n, 0, objs, pts)
' The first point found will be on the same face
' The second one will be on the face opposite
Dim pt2 As Point
Set pt2 = pts(2)
GetThickness = pt1.DistanceTo(pt2)
End Function
Sub ConvertToSheetMetal()
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
' Turn it into a sheet metal part
oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
Dim cd As SheetMetalComponentDefinition
Set cd = oDoc.ComponentDefinition
cd.UseSheetMetalStyleThickness = False
cd.Thickness.Value = GetThickness(cd.SurfaceBodies(1))
Call cd.Unfold
End Sub
It works for about 70% of my plates. I'm guessing the issue might be related to the location of my origins - all of our part files are derived from a single "skeleton" part file where we do all of our modeling. So, the origin is rarely on or near the part file.
I've attached two seemingly identical part files. their only real difference is their relationship to the origin. The code works correctly for one part and not the other. Can anyone help me understand why that is or provide a solution to this problem?
Solved! Go to Solution.