How do I get the name of the part, assembly or mep in progress?

Hello

How can I retrieve the names of my parts, mep and assembly in order to close them after registration? 

myTitle = myModel.GetTitle

swApp.ActivateDoc2 myTitle, True, 0

swApp.QuitDoc myTitle

I tried this but it doesn't work. I also can't find the command to simply save my assembly.

2 Likes

Hello,

 

It depends, do you want to recover the name of the active file? That is to say when you are on a plan you want to retrieve the name of the plan, in an assembly the name of the assembly, and in a part the name of the part?

If so, this tutorial I just created on Lynkoa will be fine:

http://www.lynkoa.com/tutos/import-export-formats-neutres/vba-solidworks-nom-fichier

 

To register an assembly without changing all the parts, you have to start with the "highest" assembly until you open the S.A. and the parts they contain. That's why from the assembly I open my part, the mep and I save under and then close the document but the operator changes the name between two that's why I need it in order to close the document. Then I have to find a way to save the highest assembly after the changes.

I hope I'm clear, in the attached code I can't close the drawing to go back to the part for example.


macro50.swp

Can you put your code in a text file please?

Set swapp = _
Application.SldWorks

Set Part = swapp. OpenDoc6("\xxxxxxxxxx", 2, 0, "", longstatus, longwarnings)
swapp. ActivateDoc2 "Pot Store Set", False, longstatus
Set Part = swapp. ActiveDoc

F1. Show

Set swappp = Application.SldWorks
Set swmodel = swappp. ActiveDoc
bool = swmodel. Extension.RunCommand(SwCommands.swCommands_SaveAs, "")

'swapp. OpenDoc(Name,Type) '
      
Set Part = swapp. ActiveDoc
boolstatus = Part.Extension.SelectByID2("Pots-Store 1@Ensemble Store pots_9", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.OpenCompFile
Set Part = swapp. OpenDoc6("\xxxxxxxx", 1, 0, "", longstatus, longwarnings)
Set Part = swapp. ActiveDoc

Set Part = swapp. OpenDoc6("\xxxxxxxx", 3, 0, "", longstatus, longwarnings)
Set Part = swapp. ActiveDoc

Set swappp = Application.SldWorks
Set swmodel = swappp. ActiveDoc
bool = swmodel. Extension.RunCommand(SwCommands.swCommands_SaveAs, "")

It looks like your code is not integer (no "sub").

What's the problem exactly? Can you open but not close the active document?

Is there a line in error when you launch your code?

I can't test, I don't have SolidWorks, so I need more information.

I have only provided you with a part of my code, I have advanced a little this morning and found solutions (my code is provided below). The problem that arises with this code is that I can't get the MEP title through the same means as parts and assemblies. Indeed this code saves the part, the mep, the assembly at the beginning but not at the end because it closes instead of my mep. As a result, references in the assembly are lost.

Sub main()
        
Set swapp = _
Application.SldWorks

Set Part = swapp. OpenDoc6("\\xxxxxx", 2, 0, "", longstatus, longwarnings)
swapp. ActivateDoc2 "Pot Store Set", False, longstatus
Set Part = swapp. ActiveDoc

F1. Show


Set swAppp = Application.SldWorks
Set swmodel = swAppp.ActiveDoc
bool = swmodel. Extension.RunCommand(SwCommands.swCommands_SaveAs, "")


'swapp. OpenDoc(Name,Type) '
      
Set Part = swapp. ActiveDoc

'Get the link
FilePath = swmodel. GetPathName
'Retrieves the number of characters
PathSize = Len(FilePath)
'Recovers file name only without extension
PathNoExtension = Left(FilePath, 29)

TitleA = swmodel. GetTitle

TitleAs = Len(TitleA)

TitleAn = Left(TitleA, TitleAs - 7)

Set Part = swapp. ActiveDoc
Set Part = swapp. OpenDoc6("\\xxxxx"", longstatus, longwarnings)
Set Part = swapp. ActiveDoc

swapp. ActivateDoc2 "Store pots.sldprt", False, longstatus
Set Part = swapp. ActiveDoc


Set Part = swapp. OpenDoc6("\\xxxxx", 3, 0, "", longstatus, longwarnings)
Set Part = swapp. ActiveDoc

swapp. ActivateDoc2 "Jar Store - Sheet1", False, longstatus
Set Part = swapp. ActiveDoc


bool = swmodel. Extension.RunCommand(SwCommands.swCommands_SaveAs, "")

Set Part = swapp. ActiveDoc

TitleM = swmodel. GetTitle

TitleMs = Len(TitleM)

TitleMn = Left(TitleM, TitleMs - 7)

Set Part = swapp. ActiveDoc
Set Part = Nothing
swapp. CloseDoc TitleMn
Set Part = swapp. ActiveDoc

swapp. ActivateDoc2 "Store pots.sldprt", False, longstatus
Set Part = swapp. ActiveDoc

Set swAppp = Application.SldWorks
Set swmodel = swAppp.ActiveDoc
bool = swmodel. Extension.RunCommand(SwCommands.swCommands_SaveAs, "")


TitleP = swmodel. GetTitle

TitlePs = Len(TitleP)

TitlePn = Left(TitleP, TitlePs - 7)


Set Part = swapp. ActiveDoc
Set Part = Nothing
swapp. CloseDoc TitlePn
Set Part = swapp. ActiveDoc

swapp. ActivateDoc2 TitleAn, False, longstatus
Set Part = swapp. ActiveDoc

Set Part = swapp. ActiveDoc
longstatus = Part.SaveAs3(FilePath, 0, 2)

Set Part = swapp. ActiveDoc
Set Part = Nothing
swapp. CloseDoc TitleAn


F2. L_2 = TitleMn

F2. Show


End Sub

What is the line that doesn't work?

To close the active document I will proceed like this:

swApp.CloseDoc(swModel.GetPathName)

Hello

Your code gives me the link to the mep, but I need the name in order to close it, am I wrong? Anyway, it always gives me the link to the assembly, I think I must have a problem activating a document when I switch from my assembly to my mep. My code works, it's just that it doesn't do what I want.

So try inserting these lines when you want to close a drawing :

Set swmodel = swapp. ActiveDoc

swApp.CloseDoc(swModel.GetPathName)

 

 

1 Like

By the way, I just saw in your code that there are sometimes "swApp" and "swAppP".

I think it's a mistake and you should replace it with "swApp"  everywhere.

To sum up, my program gets lost in the documents because I can't get the right names. Attached are two pictures of what he gets as a name and the same but with your instruction: (in 1st it's the assembly with and without extension, in 2nd it should be the mep and in 3rd the part)


err1.png

It works with your last comments that I hadn 't seen! For the swapp(p), I have two different statements, so I think I have no choice.

Dim swAppp As SldWorks.SldWorks

Dim swApp As Object

A very big thank you to you, I just have to do this for the sub-assemblies and the other parts:)

 

1 Like

No worries!

To thank me, please choose my answer as the best answer that helped you:)