Skip to content

Instantly share code, notes, and snippets.

@AmenJlili
Created December 14, 2022 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmenJlili/c040a98009908b34e66ade363bcf6b1f to your computer and use it in GitHub Desktop.
Save AmenJlili/c040a98009908b34e66ade363bcf6b1f to your computer and use it in GitHub Desktop.
BatchCreateBoundingBoxForAssemblyComponents
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swAssembly As AssemblyDoc
Dim vComponents As Variant
Dim ProcessedFiles As New Collection
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'go back and watch my first video
Set swAssembly = swModel
'create bounding box
CreateBoundingBox swModel
vComponents = swAssembly.GetComponents(False)
Dim component
For Each component In vComponents
Dim swComponent As Component2
Set swComponent = component
Dim swComponentModelDoc As ModelDoc2
Set swComponentModelDoc = swComponent.GetModelDoc2
If Not swComponentModelDoc Is Nothing Then
If ExistsInCollection(ProcessedFiles, swComponentModelDoc.GetTitle()) = False Then
CreateBoundingBox swComponentModelDoc
ProcessedFiles.Add swComponentModelDoc.GetTitle(), swComponentModelDoc.GetTitle()
Debug.Print swComponentModelDoc.GetPathName()
End If
End If
Next component
End Sub
Sub CreateBoundingBox(ByRef swComponentModelDoc As ModelDoc2)
swComponentModelDoc.Visible = True
Dim swFeatureManager As featureManager
Dim swBoundingBoxFeatureDefinition As BoundingBoxFeatureData
Dim swBoundingBoxFeature As Feature
Set swFeatureManager = swComponentModelDoc.featureManager
Set swBoundingBoxFeatureDefinition = swFeatureManager.CreateDefinition(swConst.swFmBoundingBox)
swBoundingBoxFeatureDefinition.ReferenceFaceOrPlane = swConst.swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit
swBoundingBoxFeatureDefinition.IncludeHiddenBodies = False
swBoundingBoxFeatureDefinition.IncludeSurfaces = False
Set swBoundingBoxFeature = swFeatureManager.CreateFeature(swBoundingBoxFeatureDefinition)
swComponentModelDoc.Visible = False
End Sub
Public Function ExistsInCollection(col As Collection, key As Variant) As Boolean
On Error GoTo err
ExistsInCollection = True
IsObject (col.Item(key))
Exit Function
err:
ExistsInCollection = False
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment