Import parts into an assembly with the same origin

Hello

 

I want to import two parts into an assembly and make sure that their origins are confused with that of the assembly.

 

It's something easy manually but the macro I get doesn't...

 

thank you for your help

 

Code:

Sub Assemblage()

 

Dossier_dest = "D:\TRAPRO III Project\Macro Test\"

 

Set swApp = Application.SldWorks

 

Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2012\templates\Assemblage.asmdot", 0, 0, 0)

swApp.ActivateDoc2 "Assemblage1", False, longstatus

Set Part = swApp.ActiveDoc

Dim myModelView As Object

Set myModelView = Part.ActiveView

myModelView.FrameState = swWindowState_e.swWindowMaximized

boolstatus = Part.AddComponent(Dossier_dest & name & ". SLDPRT", 0, 0, 0)

boolstatus = Part.AddComponent("D:\Project TRAPRO III\TRAPRO DESIGN +\01-Technical Library Ring\New\Crown Ring 25.SLDPRT", 0, 0, 0)

 

End sub

Look in the API help (SolidWorks menu?/API Help)

Look for the AddComponent5 command 

There are code examples that you can use 

3 Likes

I don't really see the point because when you import and you click directly on OK, it sticks the origins together.

Maybe you can:

1- Free the room

2- add a coincidence constraint on the origin of the assembly and on the origin of the part by checking align the axes (I think in my head).

3- Fix the part

4- Remove the created constraint.

 

I would like to take up this question again, which has not been marked as resolved because I am faced with a similar problem.

I want to insert parts into an assembly by first inserting a sketch (whose lines serve as references for the assembly of other parts) and I can't make the two origins coincident (via the AddComponent, AddComponent2 or even AddComponent5 functions).

Namely that I am on SolidWorks 2012.

Thank you for your help

Have you looked at this link? http://help.solidworks.com/2012/English/api/sldworksapi/Add_Component_and_Mate_Example_VB.htm

Hello

I had seen this connection before, but my attempts were not at all conclusive. However, I took my code to make it coincide with this example but an error message appears:

"Runtime Error '91':

Variable obket or variable block With not defined"

For line 100: matefeature. Name = MateName

I've attached my code if it can help understand it (but it's linked to a userform)


createass.txt

Good evening

You will find in attachments a commented macro that allows you to place two document parts and add a constraint on the origins.

To make it work for you, change the path to the parts and open a new assembly.

The macro is commented, you should understand it quite easily.

Good code.


insertion_pieces.swp
2 Likes

Thank you very much I think (I hope) that this will get me out of a nice mess!

1 Like

Hello

Yes it's me again, I'm coming back but in fact the problem hasn't been solved. I had noticed it a long time ago, but I had suddenly become attached to other functions of my program, it didn't matter.

Despite this beautiful program made by Yves T., I always come up against the same problem: my piece is always fixed from the insertion and does not add origin to origin. No matter how hard I try to fix, free the piece, turn the lines in all directions, nothing helps!

Thank you for your help.

I'm attaching my macro, which is most certainly not very clear given the number of moficiations and tests I've done.


createass_v2.txt

Hello

How do you provide the sub main parameters?

Sub main(oCabineCode As String, SelectedFile As String, TracePath As String)

You didn't test the parameters to make sure they have a value before continuing with the code.

Why reassign a value to swApp during the program?

Without any explanation on your part of what you thought you would do with:

Set swApp = _
Application.SldWorks

Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2012\templates\Assemblage.asmdot", 0, 0, 0)
Set swModel = swApp.ActivateDoc2(oCabineCode, False, longstatus)
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

longstatus = Part.SaveAs3(SelectedFile, 0, 2)

Set Part = swApp.ActivateDoc2(oCabineCode, False, errors)
Dim TmpTitle As String
TmpTitle = swApp.ActiveDoc.GetTitle
swApp.CloseDoc TmpTitle

I don't understand what it's for:)

I' ve looked at your code in a little more detail. There are quite a few mistakes. Go back to the macro I posted. It works in all cases and for a version of SW >2010.

If you already have the filenames to add to the assembly, put them either in a global variable (array of String) or in a property.

If you want to allow the user to choose which documents to insert, use: swApp.GetOpenFileName(). This will display the SW OpenFileDialog, to which you can specify several options.

Avoid re-signing variables when they don't change. As much as possible, do not re-affix value to swApp.

You can reassign a value to the other variables.

Avoid having to pass parameters to the Main() procedure  . It is better to write a second procedure that will be called by Main which will have parameters.

Style:

Sub Main()

MakeThe Calculation  Param1, Param2,Param3

End Sub

Sub FaireLeCalcul( ByVal param1 as blabla, ... )

End Sub

It's more readable and more meaningful. It also allows you to write your code better without getting mixed up, especially when you're new to programming.

To me, your code doesn't work because a variable shouldn't have the expected value. If you don't test on String variables, it can be dangerous. A variable of type String is set to "String Empty" when it is initialized. That's why in many programming languages, we test the value of a variable.

In the macro I posted, I didn't do it because you have to put a little of your :)

Kind regards.

1 Like

Hello

Thank you for the feedback. I will answer little by little the various questions you have formulated (according to my tests and modifications) and explain my program.

When I launch the program, I have a UF that opens in which the itilsator fills in the name of the assembly (oCabineCode), then the folder in which it wants to save the assembly (SelectedFile) and finally the reference element of this assembly by its path (Tracepath). With all the tests carried out I know that I have no worries about these different data. by clicking on "Create assembly" this program is launched and will therefore open a new assembly, save it as to give it the desired name and save it in the target directory. This is what this little piece of program that you extracted in your first message yesterday does. When I do this registration I actually end up with 2 assemblies "AssemblageX" and "oCabineCode" to avoid problems so I close "AssemblageX".

Despite the command "Set Part = swApp.ActivateDoc2(oCabineCode, False, errors)" otherwise I still end up with "AssemblageX" as an active part. So that's why I'm reassigning a value to swApp. It's also "simpler" for me since I can identify my assembly by its name, which I know, whereas if it's Assemblage"X" it becomes more complicated, at least it seems to me.

However, I will try to use your indications proposed in the second message to resolve this little setback.

Thank you again for your interest.

Well, I heard it right and try to follow your advice I reread, reviewed and deleted a lot of lines of my code. I checked all the data of my String variables, a priori I don't have any errors on this side either. My part fits well into my assembly but when I get to the constraint it fails again!

I end up with an error "438: Property or method not handled by this object" and when I search on the SM API help I can't find any help for the "AddMate5" function but it's this line, and it's the last one in my code, which causes the error. The component is added fixed and I can always run the UnfixComponent method, nothing to do.

At worst I do the manipulation by hand, that's what I've been doing since the beginning but it's a bit ... painful.

Hello

Do you have your 3 plans in your rooms as well as in the assembly??? I think your origin is at the intersection of the 3 planes!! Why not see with your for the constraint of the plans between them, there your parts would have the same origin and would be immediately constrained...

1 Like

My apologies, AddMate5 only appeared from Sw 2015. For SW 2012, use AddMate3.

You can find all the information for using the method in the SW API help.

Hello

Here I am again with the same concern:

I'm on SW2012 and when I add my first part in the assembly it's fixed, I manage to add constraints to make the origins and the reference planes coincide but suddenly it's overstressed (it's impossible to activate the constraints since they are applied to a fixed component). I added the following lines to the code:

Part.FixComponent or

Part.UnfixComponent

It doesn't change anything.

I tried with the first formula because when I make a macro recording it's the one that appears to release AND fix the part under SW2012.

I tried with the second one but it doesn't work either.

For the moment my program runs perfectly except on this point (so I take breaks to manually release the first parts of each sub-assembly I create, and I do 7 of them so it's a bit painful and the program loses fluidity).

If you have a solution, you would help me a lot.

Thank you in advance and again!

Hello Seagull,

If the code is still the one attached on page 1 of this topic, I think we should instead concatenate TraceName & "@" & AssemblyName into a variable and then call it.

I think that may explain the problem. Otherwise, by placing a stop in the code and scrolling in step-by-step from this point, it will allow you to check if the model is selected in the FeatureManager.

boolstatus = Part.Extension.SelectByID2(TraceName & "@" & AssemblyName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.UnfixComponent

 

1 Like

It's always the same code.

Thank you Cyril, this solves my problem!

it was mostly missing the increment in the name of the element after "TraceName"

boolstatus = Part.Extension.SelectByID2(TraceName & "-1@" & AssemblyName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.UnfixComponent

Now it works perfectly, thank you again!