Is there a way generate a custom menu button, and run a macro that will automatically mate a selected part / assembly to the right and front planes?
I mate so many things to these planes and would love a way to have it automated.
Don't worry I dont want someone to write it for me, but if it's possible, could someone nudge me in the right direction?
I know nothing about API, am a little versed in VBA but no clue about VB... but willing to learn!!
SolidworksApi macros
Hi Trev,
One of the easiest ways to figure out how to code something when first starting out is to simply record a macro of the process that you want to duplicate. Once you have that basic Idea you just need to tweek the code to get it to do what you want. The macro that you want to create is pretty straight forward. The hardest part will be getting the two planes selected in the code.
Here is some code to help point you in the right direction:
'set the names of the planes
FirstSelection = "Right@" + FirstComponentName & "@" + AssemblyName
SecondSelection = "Right@" + SecondComponentName & "@" + AssemblyName
' Select the planes for the mate
boolstatus = Part.Extension.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
' Add the mate
Set matefeature = assydoc.AddMate3(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, longstatus)
Good Luck,
Rob
Thanks Robert!
I am now well on my way, just having problems making it work on any highlighted part but I will get there.
What do you usually write code in? Microsoft Visual Basic?
And more importantly. Is it possible to produce an icon on the toolbar that triggers the macro?
If I have to goto tools/macro/run/browse everytime then I might as well do it manually lol.
Thanks for your input Robert. most helpful!
Trev,
To add a button to a toolbar:
1. With a file open (any file) right click on a toolbar and select customize.
2. Go to the "Commands" tab and select "Macro" from the list on the left.
3. Drag the icon of the litte person in a green shirt to the place where you want your button to reside (on some toolbar)
4. In the window that pops up; Click on "Choose Image" and browse to the location of the bitmap that you want to use as your icon
5. Click on the "..." button next to macro to navigate to the location of your macro (.swp) file.
6. Make sure the the "Method" matches up with your main subroutine in the macro file. (Probably "MacroName1.main")
7. Click ok and ok, and you're done!
These days I do most of my programming in VB.net either through Visual Studio or the VSTA interface through SolidWorks. In my experience, there are more rules in VB.net and it is less forgiving then VBA. However, once you have it figured out it is easier to use.
Good Luck.
Rob
Thanks Robert.
Really really really appreciated.
I will stick with VB for now as I've used it before, then progress as my skill level does.
Notice an odd thing already though. When I record a macro it coughs up this code.
Dim pwPhotoWorks As PhotoWorks.PhotoWorks
Set pwPhotoWorks = swApp.GetAddInObject("PhotoWorks.PhotoWorks")
Dim pwOpt As PhotoWorks.PwOptions
Set pwOpt = pwPhotoWorks.PwOptions
I have to delete it everytime and quite annoying lol.
Well i have enough to get on with now. Will let you know how I get on.
Thanks again.
Trev
OK am getting there.
So far I have 2 buttons. I have to select the appropriate plane on the part, then click on the button.
I'm struggling to understand the code.
I still want to do this myself, but can someone tell me what of the following code is not needed or explain what teh code is doing so I can understand it better and write the code that I need?
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
Dim myMate As Object
Set myMate = Part.AddMate3(0, 0, False, 0, 0, 0, 0.001, 0.001, 0.09374195175402, 0.5235987755983, 0.5235987755983, False, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3
boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Coincident18")
End Sub
Many Thanks
Trev
Hey Trev,
I'll try and explain to you what's going on in your code...
'This is where solidworks is assigned to the variable swApp
Set swApp = Application.SldWorks
'Here, your active doc is assigned to the variable Part
Set Part = swApp.ActiveDoc
'Here you are selecting an entity by an identification means, where Top Plane is the name as plane is the Type
boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
'Here a dimension is assigned to the variable myMate, and it is simply identified as an object
Dim myMate As Object
'Here you are assigning the return value of the AddMate3 function to the variable myMate, which is a mate2 type object. So technically, the above dimensioning of myMate should be to the type of Mate2 and not Object.
Set myMate = Part.AddMate3(0, 0, False, 0, 0, 0, 0.001, 0.001, 0.09374195175402, 0.5235987755983, 0.5235987755983, False, longstatus)
'Here you're using the ClearSelection2 Method with a parameter of True, which clears the entire existing selection list...
Part.ClearSelection2 True
'Here the model is rebuilt...
Part.EditRebuild3
'Here you're just setting the feature properties...(honestly don't even know why this would be recorded...doesn't seem like anything was done)
boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Coincident18")
Anyway, the approach I would try and take would be...
Make a macro that you can just select two any entities of the two parts you wish to mate in the way...the macro then would discover what components in the assembly they belonged to, and next select the respective planes and mate them together, this way, there's no need to pre-select the planes...
If you need help with this, let me know..
cheers
Filipe
Thanks Filipe
That helped me understand alot!
Thanks to all for patience with a complete beginner
Right... So far I have a working Macro that I can simply select from the toolbar and it will run.
Here is my code. (pretty unchanged from the recorded macro, except I entered the front plane mate by hand and it worked... phew!
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Right Plane@Part1-1@Assem1", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
Dim myMate As Object
Set myMate = Part.AddMate3(0, 0, False, 1.148190722917, 0, 0, 0.001, 0.001, 0, 0.5235987755983, 0.5235987755983, False, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3
boolstatus = Part.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Coincident10")
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Front Plane@Part1-1@Assem1", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
Set myMate = Part.AddMate3(0, 0, False, 0.0511634756676, 0, 0, 0.001, 0.001, 0, 0.5235987755983, 0.5235987755983, False, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3
End Sub
So far it only works on the specific part created to build the macro. I've highlighted the code in red, that I know I need to change. But what do I replace it with to allow me to select what part I want the macro to work on? I've trawled t'internet but am lost
Trev
Hi Trev,
I was able to get this to work. I added all of the red bits. The dims at the top aren't strictly neccessary, but they are a very good idea and generally good coding practice. The rest of the code gets the selected component, and then builds a name string to be used in the selection of the planes. As a side note; to get this to run on my system I had to change "Right Plane" in the selection lines to just "Right" (same thing with the front plane). Since "Right Plane" worked previously on your system you should be fine. Let me know if you have any more questions. Good Luck!
Dim swApp As SldWorks.SldWorks
Dim assy As SldWorks.AssemblyDoc
Dim part As SldWorks.Component2
Dim myMate As SldWorks.Mate2
Dim selmgr As SldWorks.SelectionMgr
Dim selEntity As SldWorks.Entity
Dim PartName As String
Dim longstatus As SwConst.swAddMateError_e
Sub main()
Set swApp = Application.SldWorks
Set assy = swApp.ActiveDoc
Set selmgr = assy.SelectionManager
If selmgr.GetSelectedObjectCount > 0 Then
Set selEntity = selmgr.GetSelectedObject5(1)
Set part = selEntity.IGetComponent2
PartAndAssyString = "@" & part.Name2 & "@" & Split(assy.GetTitle, ".")(0)
assy.ClearSelection2 True
boolstatus = assy.Extension.SelectByID2("Right Plane" & PartAndAssyString, "PLANE", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = assy.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
Set myMate = assy.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, longstatus)
assy.ClearSelection2 True
assy.EditRebuild3
'boolstatus = assy.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, 1, 0, "Coincident10")
boolstatus = assy.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = assy.Extension.SelectByID2("Front Plane" & PartAndAssyString, "PLANE", 0, 0, 0, True, 1, Nothing, 0)
Set myMate = assy.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, longstatus)
assy.ClearSelection2 True
assy.EditRebuild3
Else
MsgBox "Nothing Selected", vbOKOnly, "Auto-Mate"
End If
End Sub
Robert. Sorry been in meetings all afternoon.
Thank you!!
It doesn't do what I need it to do, as the need for this has changed slightly.
But!! it has crossed all the T's, dotted al the i's, and given me the snippets of code that I need to pull it off.
That way I get to learn other than just use someone elses. I can break down the code and recylcle it
Happy days.
Thaks again, will post up the result IF I manage it