CatiaV5 macro: how to insert background view?

2019-08-25 03:22发布

问题:

I'm making a macro to automatically open a new drawing on the correct sheet format with filled in title block but I can't seem to figure out how to insert a pre-made .CATDrawing in the same way the following option in the page setup dialog box would:

see here: https://i.imgur.com/goClGIh.png

my current progress looks like this:

Sub CATMain()

Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument

Dim myParam As Parameter
Set myParam = partDoc.Part.parameters.Item("Description")

Dim documents1 As Documents
Set documents1 = CATIA.Documents

Dim MyDrawingDoc As DrawingDocument
Set MyDrawingDoc = documents1.Add("Drawing")

MyDrawingDoc.Standard = catISO

Dim MyDrawingSheets As DrawingSheets
Set MyDrawingSheets = MyDrawingDoc.Sheets

Dim MyDrawingSheet As DrawingSheet
Set MyDrawingSheet = MyDrawingSheets.Item("Sheet.1")

MyDrawingSheet.PaperSize = catPaperA3

MyDrawingSheet.[Scale] = 1#

MyDrawingSheet.Orientation = catPaperLandscape

**CATIA.StartCommand "Page Setup"**

Dim dView As DrawingViews
Set dView = MyDrawingSheet.Views
dView.Item("Background View").Activate

AddTextWithLinkedParameter dView, 20, 20, myParam

End Sub

Sub AddTextWithLinkedParameter(dViewToContainTheText As DrawingViews, xPos, yPos, Optional param As Parameter)

Dim dtext As DrawingText
Set dtext = dViewToContainTheText.ActiveView.Texts.Add("", xPos, yPos)

If Not param Is Nothing Then
dtext.InsertVariable 0, 0, param
End If

End Sub

This line here

CATIA.StartCommand "Page Setup"

should be replaced by a sequence of codes that does the same thing as clicking the options would as shown in the image above.

回答1:

In my experience, I think you are better off writing a script to draw a title block rather than using a template. This way it's more flexible with regards to changing sheet size and orientation. You can also update the titleblock if sheet size and orientation changes. This is also how catia does titleblocks with the catscript. I would avoid StartCommand as it's not inline with the script execution.

That being said. If you want to use a "template", then the best way to do that is to setup your template catDrawing and then your script will open the template as read-only, do what you need, and then the user will save-as. Avoid StartCommand if you can.



回答2:

Directly opening the .CATdrawing template has the same result.
One can do this by using the follwing code:

Dim documents1 As Documents
Set documents1 = CATIA.Documents

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


标签: vba catia