Instructions
This macro listens for swAssemblyAddItemNotify when a user drops a component from the task pane. The component is then saved to the same location as the assembly.
- Preconditions
- Assembly is open
- Result
- See smart messages
This is not an ideal solution for toolbox because the user never has a chance to choose a configuration. Consequently the first configuration gets chosen by default.
The "Failed to open file" occurs when the user attempts to insert a component that was already previously added while this macro was running and if the parent assembly was never re-opened in-between additions. (In other words, run this macro and then try inserting the same component two times in a row.)
Files
Save component after dropped from task pane
Code
Module: mMain
'This macro listens for swAssemblyAddItemNotify when a user drops a component from the _
task pane. The component is then saved to the same location as the assembly.
'Preconditions: Assembly is open
'Postconditions: See smart messages
'This is not an ideal solution for toolbox because the user never has a chance to choose _
a configuration. Consequently the first configuration gets chosen by default.
'The "Failed to open file" occurs when the user attempts to insert a component that was _
already previously added while this macro was running and if the parent assembly was _
never re-opened in-between additions. (In other words, run this macro and then try _
inserting the same component two times in a row.)
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Dim swApp As SldWorks.SldWorks
Dim add_item_listener As New clsAddItemListener
Sub main()
Set swApp = Application.SldWorks
add_item_listener.connect swApp
End Sub
Class: clsAddItemListener
Dim swApp2 As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim WithEvents swAssy As SldWorks.AssemblyDoc
Dim blnSecond As Boolean
Public Sub connect(swApp As SldWorks.SldWorks)
Set swApp2 = swApp
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
swModel.Extension.ShowSmartMessage "Listener on.", 5000, False, True
Debug.Print "listener on"
blnSecond = False
End Sub
Private Function swAssy_AddItemNotify(ByVal EntityType As Long, ByVal itemname As String) As Long
If blnSecond = False Then 'see notes
swApp2.RunCommand swCommands_PmOK, Empty
Dim strFilePath As String
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim strCompName As String
Dim oFSO As FileSystemObject
Dim lngWarning As Long, lngError As Long
strCompName = Left(itemname, InStrRev(itemname, "-") - 1)
strFilePath = swModel.GetPathName
If strFilePath <> "" Then
strFilePath = Left(strFilePath, InStrRev(strFilePath, "\"))
strFilePath = strFilePath & strCompName & ".SLDPRT"
Set oFSO = New FileSystemObject
If oFSO.FileExists(strFilePath) = False Then
Set swComp = swAssy.GetComponentByName(itemname)
Set swCompModel = swComp.GetModelDoc2
swCompModel.Extension.SaveAs strFilePath, swSaveAsCurrentVersion, _
swSaveAsOptions_Silent + swSaveAsOptions_Copy, Nothing, lngError, lngWarning
Debug.Print "Saving: ", lngError, lngWarning
If lngError <> 0 Then
swModel.Extension.ShowSmartMessage "File failed to save." & vbCrLf & _
"Error: " & lngError & vbCrLf & "Warning: " & lngWarning, 5000, False, True
Else
swModel.Extension.ShowSmartMessage "File saved to " & strFilePath, 5000, False, True
swComp.Select4 False, Nothing, False
swModel.Extension.DeleteSelection2 Empty
Call AddComp(strFilePath, itemname)
End If
Else
swModel.Extension.ShowSmartMessage "File already exists.", 5000, False, True
Set swComp = swAssy.GetComponentByName(itemname)
swComp.Select4 False, Nothing, False
swModel.Extension.DeleteSelection2 Empty
Call AddComp(strFilePath, itemname)
End If
Set oFSO = Nothing
Else
swModel.Extension.ShowSmartMessage "Assembly not saved therefore part not saved.", _
5000, False, True
End If
End If
End Function
Private Sub AddComp(strFilePath As String, itemname As String)
Dim lngError As Long, lngWarning As Long
swApp2.DocumentVisible False, swDocPART
swApp2.CloseDoc itemname
swModel.EditRebuild3 'see notes
swApp2.OpenDoc6 strFilePath, swDocPART, 0, Empty, lngError, lngWarning
Debug.Print "Opening: ", lngError, lngWarning
blnSecond = True
swAssy.AddComponent4 strFilePath, Empty, 0, 0, 0
swApp2.DocumentVisible True, swDocPART
End Sub
'Notes
'-After you close a document using ISldWorks::CloseDoc or ISldWorks::QuitDoc, you _
must call IModelDoc2::EditRebuild3 for the current IModelDoc2 object to recognize _
that the document is no longer in memory. Otherwise, when calling _
ISldWorks::OpenDoc6 you will get error 65536, meaning that a document with the same _
name is already open. In that case, IAssemblyDoc::AddComponent4 will fail as well.
'-As soon as IAssemblyDoc::AddComponent4 occurs, AddItemNotify will fire again and begin _
repeating the associated. Consequently, blnSecond was added to prevent this second _
iteration of the AddItemNotify code.
'-For some reason, selections immediately after the macro runs are not visible. Tried _
using IModelDoc2::ViewZoomIn and IModelDoc2::EditRebuild3 but these had no effect.
'-Related: You can use ISldWorks::GetOpenDocumentByName or ISldWorks::GetDocuments to _
determine if a specific file is open in memory. You can use IModelDoc2::ForceReleaseLocks _
to detach the current model from the file on the drive. See the remarks for this method _
for more related methods, and important warnings.