Hi:
Currently, we are going into numerous parts and applying a bendtable, unfolding the part and applying a sketch for constraints.This allows is to assign to a length property, the width times thelength of the part. After completion, we exit the sketch, refoldthe part and hide the sketch.
Is there a way to do this programatically, concerning getting thex/y constraints of the un folded sheet metal part? It looks asthough assigning the values to the property is the easy part.
Thanks in advance for the help!
SolidworksApi macros
Currently, we are going into numerous parts and applying a bendtable, unfolding the part and applying a sketch for constraints.This allows is to assign to a length property, the width times thelength of the part. After completion, we exit the sketch, refoldthe part and hide the sketch.
Is there a way to do this programatically, concerning getting thex/y constraints of the un folded sheet metal part? It looks asthough assigning the values to the property is the easy part.
Thanks in advance for the help!
SolidworksApi macros

I started coding, but have come questions if anyone can offer ananswer..
1. How can I check to see that the part is a sheet metal part?
2. How can I get the one and only sketch under the first feature,which is the sheet metal definition and where it is flatted from?
3. In my example below, the get flattened constraints actuallysteps through all the constraints. What I am looking for is themaximum X and Y value for the sketch. I am also fine with gettingthis from the part if it is available.
4. Finally, how I can I assign the two values as a string to theLENGTH property?
5. Any other thoughts are greatly appreciated. The end goal here isto avoid drawing a sketch on every part to get this information.
Thanks in advance for the help. .NET 2008 code is below...
Public Class Form1
' Part is open
' Bend Table Selections...
Public Enum swSMBendState_e
swSMBendStateNone = 0 ' No bend state - not a sheet metal part
swSMBendStateSharps = 1 ' Bends are in the sharp state - bendscurrently not applied
swSMBendStateFlattened = 2 ' Bends are flattened
swSMBendStateFolded = 3 ' Bends are fully applied
End Enum
Private Sub cmdFlatten_Click(ByVal sender As System.Object, ByVale As System.EventArgs) Handles cmdFlatten.Click
Dim swApp As SldWorks.SldWorks =CreateObject("SldWorks.Application")
Dim swModel As SldWorks.ModelDoc2 = swApp.ActiveDoc
Dim swSelMgr As SldWorks.SelectionMgr = swModel.SelectionManager
Dim swSelData As SldWorks.SelectData = swSelMgr.CreateSelectData
Dim swFeat As SldWorks.Feature = swSelMgr.GetSelectedObject5(1)
Dim swSketchBend As SldWorks.SketchedBendFeatureData
swSketchBend = swFeat.GetDefinition
Dim swBendFeat As SldWorks.BendsFeatureData
Dim swCustBendAllow As SldWorks.CustomBendAllowance =swSketchBend.GetCustomBendAllowance
Dim i As Long
Dim bRet As Boolean
' Check to see if part is a sheet metal part
' Assign Bend Table
Dim btfile As String = "\\d2\acad\3D-SW2009\DESPATCH SW DATA\SheetMetal Gauge Tables\WILA_HRSGuageTable.xls"
swCustBendAllow.BendTableFile = btfile
' Flatten Sheet Metal Part
Dim nBendState As Long = swModel.GetBendState
Dim nRetVal As Long
If nBendState <> swSMBendState_e.swSMBendStateFlattened Then
nRetVal =swModel.SetBendState(swSMBendState_e.swSMBendStateFlattened)
' Rebuild
bRet = swModel.EditRebuild3 : Debug.Assert(bRet)
End If
' Select Sketch - Get the first and only sketch under swFeat
' Get Flattened X/Y Constraints - Overall Sheet Size!!!
Dim swSketch As SldWorks.Sketch = swFeat.GetSpecificFeature2
Dim vSketchSeg As Object = swSketch.GetSketchSegments
Dim vConstraint As Object
For i = 0 To UBound(vSketchSeg)
vConstraint = vSketchSeg(i).GetConstraints
For j = 0 To UBound(vConstraint)
Debug.Print(" SketchSegConstraint[" & i & "] = " &vConstraint(j))
Next j
Next i
' Exit edit mode and do not rebuild the model
swModel.InsertSketch2(False)
' Assign Constraints to Length Property as X + " X " + Y
' Fold Sheet Metal Part
nRetVal =swModel.SetBendState(swSMBendState_e.swSMBendStateFolded)
' Rebuild
bRet = swModel.EditRebuild3 : Debug.Assert(bRet)
End Sub
End Class
Getting the X and Y dimensions from SolidWorks is hard! You mustfirst utilize math transforms to change the coordinate system tothe flat pattern system. Otherwise the program will only work ifthe flat pattern happens to be in the X,Y plane.
I have written a function which will do this for you. It isdirectly copied from my own macro, which does more, and may havelines which are not related.
to use this function you need to pass the function the body of theflat part, the thickness of the part, and the volume of the body.The function will traverse the faces, and find the one which has anarea which when multiplied by the thickness will give you thevolume.
This function returns the X and Y values of the flat pattern as avariant.
Please note: This code is old and in VB. It is not perfect. If thepart is thin, yet skewed, (think of a thin strip at an angle, andnot parallel to either X, Y, or Z) then the function will returnthe smallest square the strip is on. It will however, work even ifthe flat pattern is skewed. (When flattening a conical shape theflat pattern is not relative to any front, back, or right side, andthis function will handle that.)
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
Dim dCurveStart As Double
Dim dCurveEnd As Double
Dim bIsCurveClosed As Boolean
Dim bIsCurvePeriodic As Boolean
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
bResults = swCurve.GetEndParams(dCurveStart, dCurveEnd,bIsCurveClosed, bIsCurvePeriodic)
'vEdgeParameters = swEdge.GetCurveParams2
dCurveLength = swCurve.GetLength3(dCurveStart, dCurveEnd)
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
Also you will need my conversion module (which converts from metersin SolidWorks to other length units.)
Public Enum NumericConversion
' identify types of numeric conversion
' for SolidWorks all nominal units are in Meters
' there fore all conversions will be to or from Meters
inin = 1
toin = 2
inMM = 3
toMM = 4
inCM = 5
toCM = 6
inM = 7
toM = 8
inFT = 9
toFT = 10
End Enum
Function Conversion(NumericConversion, nValue As Double) As Double
' For SolidWorks all nominal units are in Meters
' there for 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