Hello, I am making the switch from lurking on the forums to asking a question. The reason for this change is that I cannot find an answer to my problem anywhere I have looked. Sorry if this is a simple request, but I have no formal training in making macros.
My problem is that I am trying to make a macro to created a derived mirror configuration of an open part file. Manually we always open an assembly, insert the part and run the mirror function, ticking off the same selections each time. I did not see any simpler existing way in SolidWorks 2016 so I decided to write a macro to do it. Below is the code I have so far, however, I continually get a "Run-time error '438': Object doesn't support this property or method" What am I doing wrong, what needs changed to make this work, or is there a simpler way to get the same result? Thanks for any help.
Dim swPart As Object
Dim swAssy As Object
Dim Result As Long
Dim ActiveConfig As Object
Dim boolstatus As Boolean
Dim errors As Long
Dim warnings As Long
Public Sub CreateMirror()
Set swApp = Application.SldWorks
swApp.Visible = True
Set swDoc = swApp.ActiveDoc
Set swPart = swApp.ActiveDoc
Set ActiveConfig = swPart.GetActiveConfiguration
Dim ConfigName As String
ConfigName = ActiveConfig.Name
Dim PartPathName As String
PartPathName = swPart.GetPathName()
PartPath = InStrRev(PartPathName, "\") + 1
PartExten = InStrRev(PartPathName, ".")
Dim PartName As String
PartName = Mid$(PartPathName, PartPath, PartExten - PartPath)
Set swDoc = swApp.NewDocument("C:\WTI-Vault\WTI Templates\Assembly.asmdot", 0, 0, 0)
Set swAssy = swApp.ActiveDoc
Dim AssyName As String
AssyName = swAssy.GetTitle
boolstatus = swAssy.AddComponent(PartPathName, 0, 0, 0)
Dim MirrorPlane As SldWorks.Feature
Set MirrorPlane = swAssy.FeatureByName("Right Plane")
Dim CompToMirror(0) As Object
Set CompToMirror(0) = swAssy.GetComponentByName(PartName & "-1")
Dim NameModifier As String
NameModifier = ""
Dim DerivedName As String
DerivedName = "_Mirror2"
Dim CompToInstance As Object
Set CompToInstance = Nothing
Dim Orientations As Object
Set Orientations = Nothing
Dim OrientAboutCoM As Boolean
OrientAboutCoM = False
Dim CreateDerivedConfigs As Boolean
CreateDerivedConfigs = True
Dim NameModifierType As swMirrorComponentNameModifier_e
NameModifierType = swMirrorComponentName_Custom
Dim FileLocation As String
FileLocation = Left$(PartPathName, PartPath)
Dim ImportOptions As Integer
ImportOptions = swMirrorPartOptions_ImportSolids
Dim BreakLinks As Boolean
BreakLinks = False
Dim PreserveZAxis As Boolean
PreserveZAxis = False
Dim FlexSync As Boolean
FlexSync = False
Dim DerivedMirror As Object
DerivedMirror = swAssy.MirrorComponents3(MirrorPlane, CompToInstance, Orientations, OrientAboutCoM, _
CompToMirror, CreateDerivedConfigs, DerivedName, NameModifierType, NameModifier, _
FileLocation, ImportOptions, BreakLinks, PreserveZAxis, FlexSync)
swApp.CloseDoc AssyName
boolstatus = swPart.EditRebuild3()
End Sub
Michael,
Many of us here on the forums are also self taught. The easiest way to learn is to just step in and try.
For your question, at what point does the error occur? If you're not sure, try stepping through your code by pressing F8 until you reach the error. The other thing you can do is press "Debug" in the error dialog. That will keep the macro running and highlight the line where the code is currently, which will also most likely be where the error occurs.
I copied your code and the error occurs for me at
boolstatus = swAssy.AddComponent(PartPathName, 0, 0, 0)
This is occurring for me because the new assembly document specified 3 lines earlier doesn't actually open so on the line after that
Set swAssy = swApp.ActiveDoc
is actually setting swAssy as the current part file that is open. Obviously you can't insert a component into a part, only an assembly.
One thing of note on your code, it is better when declaring your variables to set them as the actual SW object you expect them to be. So instead of
Dim swPart As Object
you would have
Dim swPart As SldWorks.PartDoc or
Dim swPart As SldWorks.ModelDoc2
and
Dim swAssy As Object
would be
Dim swAssy As SldWorks.AssemblyDoc
Dim swAssy As SldWorks.ModelDoc2
That way if you aren't getting the expected SW object it will error at that point instead of further down the line when you try to do something with that object that isn't a part of the API for that object.
Also, I created an SPR a couple of years ago to allow a user to create a mirrored derived configuration of a part without having to insert it into an assembly. It is SPR #838809. Here is the link:
https://customerportal.solidworks.com/eservice_enu/start.swe?SWECmd=InvokeMethod&SWEMethod=GotoRecord&SWEService=SWGotoRecord&ViewName=SW+Defect+ER+Search+List+View+(eService)&BusObject=Product+Defect&BusComp=Product+Defect&Id=1-364DURJ&SRN=
Good luck and happy programming.