Search code examples
vbacatia

catia vba change activedocument (drawingdocument) name in session


I have a .CATdrawing template that I use to create drawings for all my parts. My current macro opens the template as desired. What I would like to do is to change the working name of the document so that when the user clicks "save" the correct name is already in the dialog box and he only needs to browse to the correct location.

To clarify I'll add an image: https://i.sstatic.net/SBqOG.png

In this image the text "Drawing2" needs to change to whatever I want it to be.

I do not want to save the .CATdrawing at this moment, the user must be allowed to continue work and save the document when it suits him.

I've been doing some searches on google and in the V5Automation.chm but I can't seem to find the code that does this.

My best guess was to try the following code:

'remember currently opened part.
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument

Dim documents1 As Documents
Set documents1 = CATIA.Documents

'Open drawing
Dim mydrawingdoc As DrawingDocument
Set mydrawingdoc = documents1.Open("X:\Path\Template.CATDrawing")

'rename drawing
Set CATIA.ActiveDocument.Name = partDoc.Part.Parameters.Item("CUSTOM_NAME").ValueAsString

However I get an error saying

Invalid use of property

Any help would be greatly appreciated.

EDIT:

I've tried a few more things to do this but as of yet I've seen no success:

Left(mydrawingdoc.FullName, 10) = partDoc.Part.UserRefProperties.Item("CUSTOM_NAME").ValueAsString

this won't work either

EDIT2:

Ok so I found something that will give a completely different error:

mydrawingdoc.FullName = partDoc.Part.UserRefProperties.Item("CUSTOM_NAME").ValueAsString

Can't assign to read-only property

Does this mean it's impossible to do what I want?

Could a possible solution be to use NewFrom instead of Open like this:

 Set mydrawingdoc = documents1.NewFrom("path\Template.CATDrawing")

And then immediately use the desired name while creating this drawingdocument?


Solution

  • It is not possible to change the name of a top-level document that has not been saved yet.
    The best and only workaround, as far as I could find, is saving the document with the correct name in a temporary folder.

    Example:

    'remember currently opened part.
    Dim partDoc As PartDocument
    Set partDoc = CATIA.ActiveDocument
    
    Dim documents1 As Documents
    Set documents1 = CATIA.Documents
    
    'Open drawing
    Dim mydrawingdoc As DrawingDocument
    Set mydrawingdoc = documents1.NewFrom("path\Template.CATDrawing")
    
    'Save drawing with custom name extracted from 3D part 
    CATIA.ActiveDocument.SaveAs ("C:\CATIA_temp\" & partDoc.Product.UserRefProperties.Item("CUSTOM_NAME").ValueAsString & ".CATDrawing")