It’s not worse. It could be more complexity than necessary for a lot of applications but if it works in your program the way it is then I’d leave it in there the way it is.
Ah, You’re writing your own design checker. Nice!
I did some tests, maybe they are not complete, I could not do other situations except for very trivial ones (when the part is completely empty, etc.). I even created a derived part based on the current one, transferring the sheet metal settings, but got the same exact results.
What is the essence of the tests? I wrote 4 functions that check in different ways whether a given part is sheet metal. And the method proposed by AlexB turned out to be the most stable in all situations. The remaining functions gave erroneous results in certain conditions.
Here is the text of the testing Module.
Sub main()
Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Debug.Print "IsSheetMetalByGetBendState = " & IsSheetMetalByGetBendState(swModel)
Debug.Print "IsSheetMetalByBodies = " & IsSheetMetalByBodies(swModel)
Debug.Print "IsSheetMetalByFeatTypeName = " & IsSheetMetalByFeatTypeName(swModel)
Debug.Print "IsSheetMetalByFlatPatternFolder = " & IsSheetMetalByFlatPatternFolder(swModel)
End Sub
'#1
Function IsSheetMetalByGetBendState(ByVal swModel As SldWorks.ModelDoc2) As Boolean
Dim nBendState As Long
nBendState = swModel.GetBendState
If nBendState <> swSMBendStateNone Then 'swSMBendStateNone = 0
IsSheetMetalByGetBendState = True
Exit Function
End If
IsSheetMetalByGetBendState = False
End Function
'#2
Function IsSheetMetalByBodies(ByVal TheModel As SldWorks.ModelDoc2) As Boolean
Dim swPart As SldWorks.PartDoc
Set swPart = TheModel
If Not swPart Is Nothing Then
Dim swBodies As Variant
swBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, False)
If Not IsEmpty(swBodies) Then
Dim i As Integer
For i = 0 To UBound(swBodies)
Dim swBody As SldWorks.Body2
Set swBody = swBodies(i)
If Not swBody Is Nothing Then
If swBody.IsSheetMetal() Then
IsSheetMetalByBodies = True
Exit Function
End If
End If
Next
End If
End If
IsSheetMetalByBodies = False
End Function
'#3
Function IsSheetMetalByFeatTypeName(ByVal TheModel As SldWorks.ModelDoc2) As Boolean
Dim swFeat As SldWorks.Feature
IsSheetMetalByFeatTypeName = False
Set swFeat = TheModel.FirstFeature
Do While Not swFeat Is Nothing
If UCase(swFeat.GetTypeName2()) = "SHEETMETAL" Then
IsSheetMetalByFeatTypeName = True
Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop
End Function
'#4
Function IsSheetMetalByFlatPatternFolder(ByVal prtModel As SldWorks.ModelDoc2) As Boolean
Dim featureMgr As SldWorks.FeatureManager
Dim flatPatternFolder As SldWorks.flatPatternFolder
Dim FlatPatternsArray As Variant
Set featureMgr = prtModel.FeatureManager
If Not featureMgr Is Nothing Then
Set flatPatternFolder = featureMgr.GetFlatPatternFolder
If Not flatPatternFolder Is Nothing Then
FlatPatternsArray = flatPatternFolder.GetFlatPatterns
If Not IsEmpty(FlatPatternsArray) Then
IsSheetMetalByFlatPatternFolder = True
Exit Function
End If
End If
End If
IsSheetMetalByFlatPatternFolder = False
End Function
Here are the results from Window Immediate with different initial states of the sheet metal part.
Just a part with one sheet body
IsSheetMetalByGetBendState = True
IsSheetMetalByBodies = True
IsSheetMetalByFeatTypeName = True
IsSheetMetalByFlatPatternFolder = True
A part with one sheet body from which the body has been removed (delete body command)
IsSheetMetalByGetBendState = False
IsSheetMetalByBodies = False
IsSheetMetalByFeatTypeName = True 'gives the wrong value
IsSheetMetalByFlatPatternFolder = False
A part that has a Sheet Metal folder but does not have any Features for creating bodies (an empty tree down)
IsSheetMetalByGetBendState = False
IsSheetMetalByBodies = False
IsSheetMetalByFeatTypeName = False
IsSheetMetalByFlatPatternFolder = False
Tree of part which was rolled above the Sheet metal folder
IsSheetMetalByGetBendState = True
IsSheetMetalByBodies = False 'gives the wrong value
IsSheetMetalByFeatTypeName = True
IsSheetMetalByFlatPatternFolder = False 'gives the wrong value
'Method #1 is best
One more question about sheet metal and VBA API deployments. What is the best way to programmatically get the dependent configuration with the FLAT-PATTERN state of the sheet metal part. I have a method that works, but it is very crude, based on passing incorrect parameters to an old function that throws an error - but does what it needs to do.
The point is that I apply the function ExportFlatPatternView to the ModelDoc2 Sheet Metal Parts object:
which checks sheet metal part is enabled to be saved in its flattened state to a DXF/DWG file at the specified path and filename.
However, I give her a path to nowhere and even with “forbidden characters” so that she definitely won’t succeed. As expected, it throws an error (which I suppress), but as a result we have a created dependent configuration to the active one, and with Sheet Metal FLAT-PATTERN.
Here is the code for my version:
Sub main()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
MakeFlatPatternSubconfForActiveCong swModel
End Sub
Sub MakeFlatPatternSubconfForActiveCong(sheetMetalModel As SldWorks.ModelDoc2)
On Error Resume Next
sheetMetalModel.ExportFlatPatternView "X:\just_for_make_SM-FLAT-PATTERN_config<>?|", 0
On Error GoTo 0
End Sub
There is one line here - the result of stability. Can anyone suggest a more correct option?
ExportToDWG2
A problem that can damage your DXF files when using the ExportToDWG2 function.
When saving a sheet metal outline in a flat form, artifacts may appear: superimposed duplicate lines, or slightly shifted ones, which create unclosed or intersecting outlines. Visually, such errors are impossible to find. They will appear when your DXF begin to be processed on a CNC machine.
The reason for such artifacts is the previously enabled display of the model in the editor window in the “Perspective” mode. If you write a macro for automatic saving of DXF files, you need to add a check or forcibly turn off Perspective always.
You should study the use of the function: RemovePerspective