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).
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)
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.
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' 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 :)
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.
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.
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...
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 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.