Macro Inserting a Component

Hi all

 

I'm working on Solidworks 2017.

I'd like to make my life a little easier. I regularly insert screws into my assemblies.

Do any of you have a macro in stock that allows you to insert a well-defined part into an assembly?

The goal is to be able to insert my screws using my 3d mouse without having to move my standard mouse too much.

 

A big thank you to you!!

 

Fred

Hello, if you just want a simple macro that adds a coin: 

 

Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim filePath As String


Sub main()

Set swApp = Application.SldWorks

Set swPart = swApp.ActiveDoc

'------ Entrer le chemin vers la pièce a ajouter -------

filePath = "  "

'--------------------------------------------------------

Dim swInsertedComponent As Component2

Set swInsertedComponent = swPart.AddComponent5(filePath, 0, "", False, "", 0, 0, 0)

End Sub

Edit: Typo fixed

1 Like

Hello

Thank you for that help.

But being super bad at macro, I think I missed somewhere.

 

Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim filePath As String


Sub main()

Set swApp = Application.SldWorks

Set swPart = swApp.ActiveDoc

filePath = "U:\PLANOTHEQUE\3D (SolidWorks)\Library\Fasteners\Screws\Fully Threaded H Screws\M12  H Screws"

Dim swInsertedComponent As Component2

Set swInsertedComponent = swPart.AddComponent5(filPath, 0, "", False, "", 0, 0, 0)

End Sub
 

 

I entered this code into the macro editor. When I execute it, I do have a part that is inserted, but it's not the right one, it's the first part of the assembly.

Could you tell me where my mistake is?

Ah, it seems that I made a typo in the next line, I had forgotten an e :

Set swInsertedComponent = swPart.AddComponent5(filePath, 0, "", False, "", 0, 0, 0)

 

Try to see

1 Like

It doesn't work.

On the other hand, in the macro editor, I now have a window that appears (see attachment)


macro.png

that's normal, it asks which function it should launch, but here we only have one function (main) so we just click on "Run". It only happens in the macro editor, when you have a button or a shortcut there won't be a problem.

 

1 Like

When I launch it, nothing happens:( 

When you have your assembly open, you do "Tools -> Macro -> Run... " and you launch the macro it doesn't work? With me no problem, so it must come from the path you took, try with another room to see. 

 

Oh by the way, the part is added to the coordinates 0,0,0 with respect to the reference frame of the assembly. Maybe it's hidden by the assembly.

1 Like

We are progressing, I manage to get a nut!

I noticed that I forgot to add the file extension (.sldprt).

On the other hand, for the screw it doesn't work yet....

I finally managed to get my macro to work for a screw, not the right one yet, but I'll get there!

 

Thank you so much for the help.

1 Like

So the error must come from the path, maybe a character that is causing a problem, I don't know.

On the other hand, I think there is better than a macro to add your screws. Maybe take inspiration from the operating principle of the toolbox, with sliding screws deposited in the assembly that are put in place with constraint references. I've never done one but you should save a lot of time if it's set up correctly. 

Some avenues of research: 

http://help.solidworks.com/2013/french/solidworks/sldworks/c_Smart_Components.htm

https://www.youtube.com/watch?v=tkVG3dEWGQk

https://www.youtube.com/watch?v=zNM0_UUdN_M&t=3813s (the interesting feature is from 44:50)

 

 

 

 

http://help.solidworks.com/2012/french/SolidWorks/sldworks/HIDD_DVE_POS_GROUP.htm?id=560a118e0a564df19e9f25d4bad8836b

http://help.solidworks.com/2016/french/solidworks/sldworks/c_example_mate_reference_multiple_entities.htm