Hi,
The code pasted below is to get the length and width of a part andI have been having alot of trouble with it and was wondering ifsomebody could help me with it.
I have been playing with this code for quite some time now and justcannot figure out what is wrong. It errors out at "Set SwConfig =swConfigMgr.ActiveConfiguration" and also with the math utility. Ifound this code on this site but it was not complete so I definedalmost all the variables so i might have made a mistake there.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeature As SldWorks.Feature
Dim swSheetMetal As SldWorks.SheetMetalFeatureData
Dim swBody As SldWorks.Body2
Dim swMassProp As SldWorks.MassProperty
Dim SwConfig As SldWorks.Configuration
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swModelExtension As SldWorks.ModelDocExtension
Dim swCustomPropertyManager As SldWorks.CustomPropertyManager
Dim swConfigurationFlat As SldWorks.Configuration
Dim swModelExt As SldWorks.ModelDocExtension
Dim swMassProperty As SldWorks.MassProperty
Dim swPart As SldWorks.PartDoc
Dim lResults As Long
Dim dThickness As Double
Dim vResults As Variant
Dim dVolume As Double
Dim vConfig As Variant
Dim sResults As String
Dim bresults As Boolean
Dim vBody As Variant
Dim sMaterial As String
Dim dHoriz As Double
Dim dVert As Double
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeature = swModel.FirstFeature
Do Until swFeature Is Nothing
If swFeature.GetTypeName = "SheetMetal" Then Exit Do
Set swFeature = swFeature.GetNextFeature
Loop
Set swSheetMetal = swFeature.GetDefinition
dThickness = swSheetMetal.Thickness
Set swModelExtension = swModel.Extension
Set swMassProp = swModelExtension.CreateMassProperty
Set SwConfig = swConfigMgr.ActiveConfiguration
lResults = SwConfig.GetChildrenCount
If lResults = 0 Then Exit Sub
vConfig = SwConfig.GetChildren
Set swCustomPropertyManager = SwConfig.CustomPropertyManager
Set swConfigurationFlat = vConfig(0)
Dim S As Integer
If UBound(vConfig) > 0 Then
sResults = swConfigurationFlat.Name
bresults = False
For S = 1 To Len(sResults) - 5
If Mid(sResults, S, 4) = "FLAT" Then
bresults = True
Exit For
End If
Next S
If bresults = False Then
Set swConfigurationFlat = vConfig(1)
End If
End If
swModel.ShowConfiguration2 (swConfigurationFlat.Name)
Set swModelExt = swModel.Extension
Set swMassProperty = swModelExt.CreateMassProperty
Set swPart = swModel
dVolume = swMassProperty.Volume
vBody = swPart.GetBodies2(swSolidBody, True)
Set swBody = vBody(0)
vResults = GetOutsideDimensions(swBody, dThickness, dVolume)
dThickness = Conversion(toin, dThickness)
sMaterial = swPart.MaterialIdName
dHoriz = Round(vResults(0) + 0.499, 0)
dVert = Round(vResults(1) + 0.499, 0)
End Sub
'You will also need the function GetOutsideDimensions
Function GetOutsideDimensions(swBody As SldWorks.Body2, dThicknessAs Double, dVolume As Double) As Variant
Dim swFace As SldWorks.Face2
Dim dArea As Double
Dim Normal As Variant
Dim dTrans(15) As Double
Dim vTrans As Variant
Dim dPoint(2) As Double
Dim vPoint As Variant
Dim swMathUtil As SldWorks.MathUtility
Dim swMathTrans As SldWorks.MathTransform
Dim swMathPoint As SldWorks.MathPoint
Dim vCorner1 As Variant
Dim vCorner2 As Variant
Dim vStartPoint As Variant
Dim vEndPoint As Variant
Dim vCenter As Variant
Dim dRadius As Double
Dim dAngle1 As Double
Dim dAngle2 As Double
Dim vCurveParams As Variant
Dim vCircleParams As Variant
Dim swEdge As SldWorks.Edge
Dim vEdges As Variant
Dim swLoop As SldWorks.Loop2
Dim vEdge As Variant
Dim dXmin As Double
Dim dXmax As Double
Dim dYmin As Double
Dim dYmax As Double
Dim bStart As Boolean
Dim Outside(1) As Double
Dim swCurve As SldWorks.Curve
Dim dResults As Double
Set swFace = swBody.GetFirstFace
Do While Not swFace Is Nothing
dArea = swFace.GetArea
If Abs((dArea * dThickness) - dVolume) < (dVolume * 0.1) Then
Exit Do
End If
Set swFace = swFace.GetNextFace
Loop
' Box = swFace.GetBox
Normal = swFace.Normal
dTrans(0) = Normal(2)
dTrans(1) = Normal(0)
dTrans(2) = Normal(1)
dTrans(3) = Normal(1)
dTrans(4) = Normal(2)
dTrans(5) = Normal(0)
dTrans(6) = Normal(0)
dTrans(7) = Normal(1)
dTrans(8) = Normal(2)
dTrans(9) = 0
dTrans(10) = 0
dTrans(11) = 0
dTrans(12) = 0
dTrans(13) = 0
dTrans(14) = 0
dTrans(15) = 0
vTrans = dTrans
Set swMathUtil = swApp.GetMathUtility
Set swMathTrans = swMathUtil.CreateTransform(vTrans)
Set swMathTrans = swMathTrans.Inverse
dCutLength = 0#
lPierceCount = swFace.GetLoopCount
Set swLoop = swFace.GetFirstLoop
Do
vEdges = swLoop.GetEdges
For Count = 0 To UBound(vEdges)
Set swEdge = vEdges(Count)
Set swCurve = swEdge.GetCurve
vEdgeParameters = swEdge.GetCurveParams2
dCurveLength = swCurve.GetLength3(vEdgeParameters(6),vEdgeParameters(7))
dCutLength = dCutLength + dCurveLength
Next Count
Set swLoop = swLoop.GetNext
If swLoop Is Nothing Then Exit Do
Loop
Set swLoop = swFace.GetFirstLoop
Do While Not swLoop Is Nothing
If swLoop.IsOuter Then Exit Do
Set swLoop = swLoop.GetNext
Loop
If swLoop.GetEdgeCount > 1 Then
vEdges = swLoop.GetEdges
bStart = False
For Each vEdge In vEdges
Set swEdge = vEdge
vCurveParams = swEdge.GetCurveParams2
dPoint(0) = vCurveParams(0)
dPoint(1) = vCurveParams(1)
dPoint(2) = vCurveParams(2)
vPoint = dPoint
Set swMathPoint = swMathUtil.CreatePoint(vPoint)
Set swMathPoint = swMathPoint.MultiplyTransform(swMathTrans)
vStartPoint = swMathPoint.ArrayData
dPoint(0) = vCurveParams(0)
dPoint(1) = vCurveParams(1)
dPoint(2) = vCurveParams(2)
vPoint = dPoint
Set swMathPoint = swMathUtil.CreatePoint(vPoint)
Set swMathPoint = swMathPoint.MultiplyTransform(swMathTrans)
vEndPoint = swMathPoint.ArrayData
If bStart = False Then
dXmin = vStartPoint(0)
dXmax = vStartPoint(0)
dYmin = vStartPoint(1)
dYmax = vStartPoint(1)
bStart = True
Else
If vStartPoint(0) < dXmin Then dXmin = vStartPoint(0)
If vStartPoint(0) > dXmax Then dXmax = vStartPoint(0)
If vStartPoint(1) < dYmin Then dYmin = vStartPoint(1)
If vStartPoint(1) > dYmax Then dYmax = vStartPoint(1)
End If
If vEndPoint(0) < dXmin Then dXmin = vEndPoint(0)
If vEndPoint(0) > dXmax Then dXmax = vEndPoint(0)
If vEndPoint(1) < dYmin Then dYmin = vEndPoint(1)
If vEndPoint(1) > dYmax Then dYmax = vEndPoint(1)
Next vEdge
Outside(0) = Conversion(toin, dXmax - dXmin)
Outside(1) = Conversion(toin, dYmax - dYmin)
GetOutsideDimensions = Outside
Else
' handle complete circle
vEdges = swLoop.GetEdges
Set swEdge = vEdges(0)
Set swCurve = swEdge.GetCurve
vResults = swCurve.CircleParams
dResults = vResults(6) * 2
Outside(0) = ConversionModule.Conversion(toin, dResults)
Outside(1) = ConversionModule.Conversion(toin, dResults)
GetOutsideDimensions = Outside
End If
End Function
'You will also need my unit conversion function
Function Conversion(NumericConversion, nValue As Double) As Double
' For SolidWorks all nominal units are in Meters
' therefore all conversions will be to or from Meters
Select Case NumericConversion
Case inin
Conversion = nValue * 0.0254
Case toin
Conversion = nValue * 39.3701
Case inMM
Conversion = nValue * 0.001
Case toMM
Conversion = nValue * 1000
Case inCM
Conversion = nValue * 0.01
Case toCM
Conversion = nValue * 100
Case inM
Conversion = nValue * 1#
Case toM
Conversion = nValue * 1#
Case inFT
Conversion = nValue * 0.3048
Case toFT
Conversion = nValue * 3.28084
Case Else
Debug.Print "Unrecognised unit type."
End Select
End Function
Thanks.SolidworksApi macros
The code pasted below is to get the length and width of a part andI have been having alot of trouble with it and was wondering ifsomebody could help me with it.
I have been playing with this code for quite some time now and justcannot figure out what is wrong. It errors out at "Set SwConfig =swConfigMgr.ActiveConfiguration" and also with the math utility. Ifound this code on this site but it was not complete so I definedalmost all the variables so i might have made a mistake there.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeature As SldWorks.Feature
Dim swSheetMetal As SldWorks.SheetMetalFeatureData
Dim swBody As SldWorks.Body2
Dim swMassProp As SldWorks.MassProperty
Dim SwConfig As SldWorks.Configuration
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swModelExtension As SldWorks.ModelDocExtension
Dim swCustomPropertyManager As SldWorks.CustomPropertyManager
Dim swConfigurationFlat As SldWorks.Configuration
Dim swModelExt As SldWorks.ModelDocExtension
Dim swMassProperty As SldWorks.MassProperty
Dim swPart As SldWorks.PartDoc
Dim lResults As Long
Dim dThickness As Double
Dim vResults As Variant
Dim dVolume As Double
Dim vConfig As Variant
Dim sResults As String
Dim bresults As Boolean
Dim vBody As Variant
Dim sMaterial As String
Dim dHoriz As Double
Dim dVert As Double
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeature = swModel.FirstFeature
Do Until swFeature Is Nothing
If swFeature.GetTypeName = "SheetMetal" Then Exit Do
Set swFeature = swFeature.GetNextFeature
Loop
Set swSheetMetal = swFeature.GetDefinition
dThickness = swSheetMetal.Thickness
Set swModelExtension = swModel.Extension
Set swMassProp = swModelExtension.CreateMassProperty
Set SwConfig = swConfigMgr.ActiveConfiguration
lResults = SwConfig.GetChildrenCount
If lResults = 0 Then Exit Sub
vConfig = SwConfig.GetChildren
Set swCustomPropertyManager = SwConfig.CustomPropertyManager
Set swConfigurationFlat = vConfig(0)
Dim S As Integer
If UBound(vConfig) > 0 Then
sResults = swConfigurationFlat.Name
bresults = False
For S = 1 To Len(sResults) - 5
If Mid(sResults, S, 4) = "FLAT" Then
bresults = True
Exit For
End If
Next S
If bresults = False Then
Set swConfigurationFlat = vConfig(1)
End If
End If
swModel.ShowConfiguration2 (swConfigurationFlat.Name)
Set swModelExt = swModel.Extension
Set swMassProperty = swModelExt.CreateMassProperty
Set swPart = swModel
dVolume = swMassProperty.Volume
vBody = swPart.GetBodies2(swSolidBody, True)
Set swBody = vBody(0)
vResults = GetOutsideDimensions(swBody, dThickness, dVolume)
dThickness = Conversion(toin, dThickness)
sMaterial = swPart.MaterialIdName
dHoriz = Round(vResults(0) + 0.499, 0)
dVert = Round(vResults(1) + 0.499, 0)
End Sub
'You will also need the function GetOutsideDimensions
Function GetOutsideDimensions(swBody As SldWorks.Body2, dThicknessAs Double, dVolume As Double) As Variant
Dim swFace As SldWorks.Face2
Dim dArea As Double
Dim Normal As Variant
Dim dTrans(15) As Double
Dim vTrans As Variant
Dim dPoint(2) As Double
Dim vPoint As Variant
Dim swMathUtil As SldWorks.MathUtility
Dim swMathTrans As SldWorks.MathTransform
Dim swMathPoint As SldWorks.MathPoint
Dim vCorner1 As Variant
Dim vCorner2 As Variant
Dim vStartPoint As Variant
Dim vEndPoint As Variant
Dim vCenter As Variant
Dim dRadius As Double
Dim dAngle1 As Double
Dim dAngle2 As Double
Dim vCurveParams As Variant
Dim vCircleParams As Variant
Dim swEdge As SldWorks.Edge
Dim vEdges As Variant
Dim swLoop As SldWorks.Loop2
Dim vEdge As Variant
Dim dXmin As Double
Dim dXmax As Double
Dim dYmin As Double
Dim dYmax As Double
Dim bStart As Boolean
Dim Outside(1) As Double
Dim swCurve As SldWorks.Curve
Dim dResults As Double
Set swFace = swBody.GetFirstFace
Do While Not swFace Is Nothing
dArea = swFace.GetArea
If Abs((dArea * dThickness) - dVolume) < (dVolume * 0.1) Then
Exit Do
End If
Set swFace = swFace.GetNextFace
Loop
' Box = swFace.GetBox
Normal = swFace.Normal
dTrans(0) = Normal(2)
dTrans(1) = Normal(0)
dTrans(2) = Normal(1)
dTrans(3) = Normal(1)
dTrans(4) = Normal(2)
dTrans(5) = Normal(0)
dTrans(6) = Normal(0)
dTrans(7) = Normal(1)
dTrans(8) = Normal(2)
dTrans(9) = 0
dTrans(10) = 0
dTrans(11) = 0
dTrans(12) = 0
dTrans(13) = 0
dTrans(14) = 0
dTrans(15) = 0
vTrans = dTrans
Set swMathUtil = swApp.GetMathUtility
Set swMathTrans = swMathUtil.CreateTransform(vTrans)
Set swMathTrans = swMathTrans.Inverse
dCutLength = 0#
lPierceCount = swFace.GetLoopCount
Set swLoop = swFace.GetFirstLoop
Do
vEdges = swLoop.GetEdges
For Count = 0 To UBound(vEdges)
Set swEdge = vEdges(Count)
Set swCurve = swEdge.GetCurve
vEdgeParameters = swEdge.GetCurveParams2
dCurveLength = swCurve.GetLength3(vEdgeParameters(6),vEdgeParameters(7))
dCutLength = dCutLength + dCurveLength
Next Count
Set swLoop = swLoop.GetNext
If swLoop Is Nothing Then Exit Do
Loop
Set swLoop = swFace.GetFirstLoop
Do While Not swLoop Is Nothing
If swLoop.IsOuter Then Exit Do
Set swLoop = swLoop.GetNext
Loop
If swLoop.GetEdgeCount > 1 Then
vEdges = swLoop.GetEdges
bStart = False
For Each vEdge In vEdges
Set swEdge = vEdge
vCurveParams = swEdge.GetCurveParams2
dPoint(0) = vCurveParams(0)
dPoint(1) = vCurveParams(1)
dPoint(2) = vCurveParams(2)
vPoint = dPoint
Set swMathPoint = swMathUtil.CreatePoint(vPoint)
Set swMathPoint = swMathPoint.MultiplyTransform(swMathTrans)
vStartPoint = swMathPoint.ArrayData
dPoint(0) = vCurveParams(0)
dPoint(1) = vCurveParams(1)
dPoint(2) = vCurveParams(2)
vPoint = dPoint
Set swMathPoint = swMathUtil.CreatePoint(vPoint)
Set swMathPoint = swMathPoint.MultiplyTransform(swMathTrans)
vEndPoint = swMathPoint.ArrayData
If bStart = False Then
dXmin = vStartPoint(0)
dXmax = vStartPoint(0)
dYmin = vStartPoint(1)
dYmax = vStartPoint(1)
bStart = True
Else
If vStartPoint(0) < dXmin Then dXmin = vStartPoint(0)
If vStartPoint(0) > dXmax Then dXmax = vStartPoint(0)
If vStartPoint(1) < dYmin Then dYmin = vStartPoint(1)
If vStartPoint(1) > dYmax Then dYmax = vStartPoint(1)
End If
If vEndPoint(0) < dXmin Then dXmin = vEndPoint(0)
If vEndPoint(0) > dXmax Then dXmax = vEndPoint(0)
If vEndPoint(1) < dYmin Then dYmin = vEndPoint(1)
If vEndPoint(1) > dYmax Then dYmax = vEndPoint(1)
Next vEdge
Outside(0) = Conversion(toin, dXmax - dXmin)
Outside(1) = Conversion(toin, dYmax - dYmin)
GetOutsideDimensions = Outside
Else
' handle complete circle
vEdges = swLoop.GetEdges
Set swEdge = vEdges(0)
Set swCurve = swEdge.GetCurve
vResults = swCurve.CircleParams
dResults = vResults(6) * 2
Outside(0) = ConversionModule.Conversion(toin, dResults)
Outside(1) = ConversionModule.Conversion(toin, dResults)
GetOutsideDimensions = Outside
End If
End Function
'You will also need my unit conversion function
Function Conversion(NumericConversion, nValue As Double) As Double
' For SolidWorks all nominal units are in Meters
' therefore all conversions will be to or from Meters
Select Case NumericConversion
Case inin
Conversion = nValue * 0.0254
Case toin
Conversion = nValue * 39.3701
Case inMM
Conversion = nValue * 0.001
Case toMM
Conversion = nValue * 1000
Case inCM
Conversion = nValue * 0.01
Case toCM
Conversion = nValue * 100
Case inM
Conversion = nValue * 1#
Case toM
Conversion = nValue * 1#
Case inFT
Conversion = nValue * 0.3048
Case toFT
Conversion = nValue * 3.28084
Case Else
Debug.Print "Unrecognised unit type."
End Select
End Function
Thanks.SolidworksApi macros

Set swConfigMgr = swModel.ConfigurationManager
I am having another error and I think it is with the math utilityif I run it errors out at "Outside(0) =ConversionModule.Conversion(swINCHES, dResults)" "Object required"Outside(0) = 0 and the Math Uitily is also = nothing. Just can'tsee what wrong somthing must not be defined properly yet. Anybodygot any ideas ? Thanks
Well not giving up I found the following code in the API help whichI think is suppose to do the trick get and Length and Width fromactive Part but It does not work.
It has few declaration problems. When I run the code I get aCompile error "User-defined type not difined" whith these 2.
Dim oOutlineCurve As OutlineCurve
Dim oOutline As Outline
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swModeler As SldWorks.Modeler
Dim swMathUtility As SldWorks.MathUtility
Dim vBodies As Variant
Dim swBody As SldWorks.Body2
Dim aVector(2) As Double
Dim vVector As Variant
Dim swVector As SldWorks.MathVector
Dim dTolerance As Double
Dim vCurves As Variant
Dim vTopologicalEntities As Variant
Dim vIndices As Variant
Dim lNumCurves As Long
Dim oOutlineCurve As OutlineCurve
Dim oOutline As Outline
Dim dictOutLines As Scripting.Dictionary
Dim lOutLineIdx As Long
Dim lIdx As Long
Dim vOutline As Variant
Dim vOutlineCurve As Variant
Dim swEntity As SldWorks.Entity
Dim swEdge As SldWorks.Edge
Dim swFace As SldWorks.Face2
Dim bValue As Boolean
Dim swCurve As SldWorks.Curve
Dim dStartParam As Double
Dim dEndParam As Double
Dim bIsClosed As Boolean
Dim bIsPeriodic As Boolean
Dim vStartPoint As Variant
Dim vEndPoint As Variant
Dim vCircleParams As Variant
Dim aCenterPoint(2) As Double
Dim vTessPts As Variant
Dim nChordTol As Double
Dim nLengthTol As Double
Dim swSketchSegment As SldWorks.SketchSegment
Dim aColours(5) As Long
nChordTol = 0.00000001
nLengthTol = 0.0000000000001
aColours(0) = RGB(255, 0, 0)
aColours(1) = RGB(0, 255, 0)
aColours(2) = RGB(0, 0, 255)
aColours(3) = RGB(255, 255, 0)
aColours(4) = RGB(255, 0, 255)
aColours(5) = RGB(0, 255, 255)
Set swApp = Application.SldWorks
Set swModeler = swApp.GetModeler
Set swMathUtility = swApp.GetMathUtility
Set swModel = swApp.ActiveDoc
Set swPart = swModel
vBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, False)
' Look along the z-axis in the negative direction;
' this corresponds to the Front view
aVector(0) = 0#
aVector(1) = 0#
aVector(2) = -1#
vVector = aVector
Set swVector = swMathUtility.CreateVector((vVector))
' Default value
dTolerance = 0.00001
lNumCurves = swModeler.GetBodyOutline((vBodies), swVector,dTolerance, vCurves, vTopologicalEntities, vIndices)
If (lNumCurves > 0) Then
Debug.Print "#curves = " & lNumCurves
Set dictOutLines = New Scripting.Dictionary
lOutLineIdx = -1
For lIdx = 0 To (lNumCurves - 1)
If (vIndices(lIdx) <> lOutLineIdx) Then
lOutLineIdx = vIndices(lIdx)
Set oOutline = New Outline
oOutline.lIndex = lOutLineIdx
dictOutLines.Add lOutLineIdx, oOutline
End If
Set oOutlineCurve = New OutlineCurve
Set oOutlineCurve.swCurve = vCurves(lIdx)
Set oOutlineCurve.swEntity = vTopologicalEntities(lIdx)
On Error Resume Next
Set swEdge = vTopologicalEntities(lIdx)
If (Not (swEdge Is Nothing)) Then
' HERE: real edge
oOutlineCurve.nType = swSelEDGES
End If
On Error Resume Next
Set swFace = vTopologicalEntities(lIdx)
If (Not (swFace Is Nothing)) Then
' HERE: silhouette edge
oOutlineCurve.nType = swSelFACES
End If
oOutline.dictCurves.Add oOutlineCurve, oOutlineCurve
Next lIdx
swModel.SetAddToDB True
swModel.SetDisplayWhenAdded False
swModel.Insert3DSketch2 False
For Each vOutline In dictOutLines.Items
swModel.ClearSelection2 True
Set oOutline = vOutline
Debug.Print "Outline " & oOutline.lIndex
Debug.Print " #curves = " & oOutline.dictCurves.Count
For Each vOutlineCurve In oOutline.dictCurves.Items
Set oOutlineCurve = vOutlineCurve
Set swCurve = oOutlineCurve.swCurve
Debug.Print " type = " & swCurve.Identity
Debug.Print " trimmed = " & swCurve.IsTrimmedCurve
Set swEntity = oOutlineCurve.swEntity
bValue = swEntity.Select4(True, Nothing)
'
' Draw some of the curves to show where they live in 3D space
'
bValue = swCurve.GetEndParams(dStartParam, dEndParam, bIsClosed,bIsPeriodic)
vStartPoint = swCurve.Evaluate(dStartParam)
vEndPoint = swCurve.Evaluate(dEndParam)
vTessPts = swCurve.GetTessPts(nChordTol, nLengthTol,(vStartPoint), (vEndPoint))
For lIdx = 0 To UBound(vTessPts) Step 3
Set swSketchSegment = swModel.CreateLine2(vTessPts(lIdx + 0),vTessPts(lIdx + 1), vTessPts(lIdx + 2), vTessPts(lIdx + 3),vTessPts(lIdx + 4), vTessPts(lIdx + 5))
swSketchSegment.Color = aColours((oOutline.lIndex ModUBound(aColours)))
Next lIdx
Next vOutlineCurve
'Stop
Next vOutline
swModel.Insert3DSketch2 True
swModel.SetAddToDB False
swModel.SetDisplayWhenAdded True
End If
End Sub
You can try just creating the objects as they are very simple. Justadd 2 new classes, one called Outline that has this code:
Public lIndex As Integer
Public dictCurves As Dictionary
Private Sub Class_Initialize()
dictCurves = New Dictionary
End Sub
And one called OutlineCurve that has this code:
Public swCurve As Curve
Public swEntity As Entity
Public nType As Long
and see if you code works then
In Class "Outline" I get an error with dictCurves "argument notoptional"
Set dictCurves = New Dictionary
After all this I find this is not the right one either. Does anybody have a bounding box macro that gets a very acurate length orwidth regardless if the part is on a plain or not that they wouldbe willing to share ?
As for VBA, I believe there is one out there somewhere thatincrements degrees to brute force find the bounding box. Trysearching the forums
I found the macro that increments brute force to find the boundingbox but if you want it to be accurate it takes a lot of time tocalculate the bounding box.
I was wondering, would it work to select a face and have the macroset the co-ordinet system to line up with the selected facetemporarily then just use one of the macros that already exists tocalculate the width and length ?
If that is too complicated, would it work to make a drawing of aflat pattern and get the bounding box of the view ?
Let me know what you think. Thanks
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.view
Dim vOutline As Variant
Dim vPos As Variant
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView
Do While Not swView Is Nothing
vOutline = swView.GetOutline
vPos = swView.position
Debug.Print "View = " + swView.Name
Debug.Print " Pos = (" & vPos(0) * 1000# & ", " &vPos(1) * 1000# & ") mm"
Debug.Print " Min = (" & vOutline(0) * 1000# & ", " &vOutline(1) * 1000# & ") mm"
Debug.Print " Max = (" & vOutline(2) * 1000# & ", " &vOutline(3) * 1000# & ") mm"
Set swView = swView.GetNextView
Loop
End Sub
Try the attached code that I found for a bounding box. When themacro is run, a note is added to the screen giving the X, Y & Zdimensions.
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim boundingBox As Variant
Dim dimension(2) As Double
Dim XValue As String
Dim YValue As String
Dim ZValue As String
Dim noteString As String
Dim XLine As Object
Dim YLine As Object
Dim ZLine As Object
Dim xDim As Object
Dim yDim As Object
Dim zDim As Object
Const conv = 39.37
Const roundingLength = 2
Sub main()
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
If Part.GetType < 1 > swDocPART Then
swApp.SendMsgToUser ("This is an assembly or drawing, not a partfile!")
Exit Sub
End If
boolstatus =Part.SetUserPreferenceToggle(swDisplayTextAtSameSizeAlways, False)
boolstatus =Part.SetUserPreferenceDoubleValue(swDetailingNoteFontHeight, 0.125/ conv)
boundingBox = Part.GetPartBox(False)
dimension(0) = Round(boundingBox(3) - boundingBox(0),roundingLength)
dimension(1) = Round(boundingBox(4) - boundingBox(1),roundingLength)
dimension(2) = Round(boundingBox(5) - boundingBox(2),roundingLength)
XValue = CStr(dimension(0))
YValue = CStr(dimension(1))
ZValue = CStr(dimension(2))
Part.Insert3DSketch
Set XLine = Part.CreateLine2(boundingBox(0) / conv, _
boundingBox(1) / conv, boundingBox(2) / conv, _
boundingBox(3) / conv, boundingBox(1) / conv, boundingBox(2) /conv)
Set YLine = Part.CreateLine2(boundingBox(0) / conv, _
boundingBox(1) / conv, boundingBox(2) / conv, _
boundingBox(0) / conv, boundingBox(4) / conv, boundingBox(2) /conv)
Set ZLine = Part.CreateLine2(boundingBox(0) / conv, _
boundingBox(1) / conv, boundingBox(2) / conv, _
boundingBox(0) / conv, boundingBox(1) / conv, boundingBox(5) /conv)
noteString = XValue & " x " & YValue & " x " &ZValue
Part.InsertNewNote3 noteString, 0, 1, 1, 0, 0, 0, 2, 1
Part.InsertSketch
Part.ClearSelection
Part.ShowNamedView2 "*Isometric", 7
Part.ViewZoomtofit2
End Sub
I thought about temporarly changing planes from what it is on toone of the main ones then it would be right but the part has to bealigned to at least 2 planes in order for that macro to workproperly. Hmmm...
If somebody has any better ideas feel free to post em.
A question I have yet, How do you select a certain sketch ? You cango and pick out sketch1 and hope that it it the base flange sketchbut chances are it is not.
Thanks