Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro To Add Additional Sheets to a Drawing 2

Status
Not open for further replies.

Cbaktis

Aerospace
May 17, 2013
13
US
Hi All,

I am very new to "macros" (it's been almost 12 years since I've played with VB scripting"). I am using a drawing template with 2 different sheet borders. I would like to write a macro that will do the following:

1. Add the new sheet
2. Add the new sheet frame (the frame is already created).
3. Modify the scale of the sheet to the same the first sheet (we use global scales for the drawing and each unique view may have there own).
4. Calculate the number of pages total and update sheet 1.

If someone can help me out that would be much appreciated.
 
Replies continue below

Recommended for you

So after some careful playing I've gotten 1, 3 and 3 working but I am having trouble with 2 and 4 working properly. I have managed to get part of 4 working (it will tally up the number of sheets) but I have already defined the variable "TOTAL SHEET". I need help with getting the script working that takes the summed total of sheets (defined as variable j) and making "TOTAL SHEET" = to this.

Still having trouble call in the title frame.

And I also having trouble creating 2 new attribute link text's (One for Part Number and One for 1 Scale).

If anyone can help out that would be great!
 
@Cbaktis
One way to get the title frame into the new sheet is simply by copying and pasting the new sheet from the sheet that already has the frame. See code below. This should take care of your requirements #1, 2, and 3. To calculate the total number of sheets simply use "drawingSheets1.Count". I would need more information to know what to do with the total number of sheets. Do you want it passed to a parameter in the tree called "TOTAL SHEET"? Is "TOTAL SHEET" then linked (using attribute link) to the text box in the title block that typically says "Sheet 1 of X" where "X" is the total sheet count? If so, that would make things very easy, because all the script would have to do is set that parameter equal to drawingSheets1.Count. The following code was written in CATIA V5's VBA Editor under Tools > Macro > Visual Basic Editor... or Alt+F11.

Code:
Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = drawingDocument1.Selection

Dim selection2 As Selection
Set selection2 = drawingDocument1.Selection

selection1.Clear
selection2.Clear

Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets

'this copies Sheet.1 - replace with whatever sheet has the frame you need
Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Item("Sheet.1")

selection1.Add drawingSheet1
selection1.Copy
selection1.Clear

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

selection1.Add drawingRoot1
selection1.Paste

'sets the scale to the same as drawingSheet1 which is "Sheet.1"
drawingSheets1.ActiveSheet.[Scale] = drawingSheet1.Scale

'this is a message box to display the total # of sheets
'we can change this to modify a parameter in the tree
'that links to the page count in your title block.
'i just need the name of the parameter in the tree.
'is it "TOTAL SHEET"?
MsgBox drawingSheets1.Count

End Sub
 
Hi drewmumaw,

Thank you for the repsonse. I'm trying to keep from "copying" the sheet as we use two different sheet frames. Sheet 1 has all the signatures and sheet 2 just has basic information (sheet number, drawing number, cage code and a revision table). There exists the possibility that there will only be 1 page drawings and a future revision will need to add a second sheet.

I've create the drawing templates as a seperate drawing and have added them into a catalog to insert them into the "drawing template". My goal is to streamline the process as automated as possible.

I was able to create a script that would do the following:

1. Total up the sheets
2. Create the sheet (with the name next in order)
3. Insert the text with the correct sheet number on the sheet
4. Set the sheet scale to a global parameter "DRAWING SCALE".

For the total number of sheets, Yes I have a define a global parameter "TOTAL SHEET" and linked that via an Attribute link to the "Sheet 1 of X" on the field of drawing. I am struggle with the code to make the Parameter "TOTAL SHEET" equal to the sheet count.

Additionally is there a way to call up the catalog and insert the sheet 2 template?

And the last thing I am struggling with is, I have gotten code working to add an attribute link to a existing text, but I am having trouble creating the text (new on the new sheet) with the attribute link (For Drawing Number and Scale).


I really do appreciate the feed back!

 
@Cbaktis
Sounds like you still need 3 things:
1. set "TOTAL SHEET" equal to the sheet count.
2. insert sheet 2 title frame from a template file (or catalog).
3. creating text on the new sheet for the drawing # and scale.

This code should take care of item #1:

Code:
Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim intParam1 As IntParam
Set intParam1 = parameters1.Item("TOTAL SHEET")

intParam1.Value = drawingSheets1.Count
 
@Cbaktis
This code should take care of item #3. You will have to change some things. For instance, if you want this text in the background, you will have to use "Background View" instead of "Main View". If your title block is in the background you will probably want this text in there too. You'll also have to change the text about "SCALE: " and "PAGE TOTAL: " as well as the location where this text is created to suit your specific title block.

Now, this code puts the text on the title block with the correct value, however, it's not linked via "attribute link". In other words, I'm not sure how to program via CATIA's API permanently linking or "attribute linking" text to a parameter. I'm not even sure if this function is exposed to automation. It may require CAA/RADE. I'll check the API after I post this, but I've never come across it before in Dassault's API documentation:

Code:
Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets1.ActiveSheet

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet2.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Main View")

Dim drawingTexts1 As DrawingTexts
Set drawingTexts1 = drawingView1.Texts

Dim drawingText1 As DrawingText
Set drawingText1 = drawingTexts1.Add("SCALE: " & drawingSheet1.Scale, 100, 200)

Dim drawingText2 As DrawingText
Set drawingText2 = drawingTexts1.Add("PAGE TOTAL: " & drawingSheets1.Count, 100, 400)
 
I just checked the API and apparently it is possible to link the newly created text to a parameter (basically automating the way you would typically right click in a text box and click "Attribute link"). Use the "InsertVariable" method. See attached image from the API documentation.

getfile.aspx


Code:
drawingText1.InsertVariable 7, 0, intParam1

where drawingText1 is the newly created textbox (probably your page # or scale)
7 is the number of characters from the start of the text box where you want the parameter (like "TOTAL SHEETS") to be inserted. This will probably be 0 for you if the parameter is the only thing in the text box.
0 is the number of characters in the text box that the parameter will replace. You'll probably keep this at 0.
intParam1 is the TOTAL SHEETS parameter.
 
For item #2 I couldn't find anything in the API about inserting a background from another drawing. To do it manually I go to File > Page Setup > Insert Background View...

There may be something for getting it from a catalog, but I'm not familiar with that. And there was nothing in the Eng-Tips FAQ section for CATIA where someone had already described how to insert a background. You could search for other threads that have discussed this issue.

As a workaround I would just open the drawing template using the following code, and then copy and paste the background using code similar to my first post above.

Code:
Sub CATMain()

Dim documents1 As Documents
Set documents1 = CATIA.Documents

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = documents1.Open("C:\Users\JohnDoe\Desktop\Template.CATDrawing")

End Sub
 
Hi drewmumaw,

Thank you for you help, I've been able to work with what you have supplied and have been able to get almost everything done that I wanted to! I'm still struggling with the the title block portion of it thought. The macro that I have written thus far creates the new sheet and add all the text that I need.

I've used what you have supplied above and gave gotten it to open the drawing template file, and then copy the entire sheet. What I think may be a better solution is to copy the feature "B SIZE SH2 FRAME.1" which is the ditto of the title block frame and then paste that object into the new drawing?

Additionally how would I control the size (height) of the new test that I've placed on the sheet, they have specific heights for the their locations in the title block.
 
@Cbaktis
I'm glad to hear that you've almost completed your script. I know it's always a big relief to put a lot of time into developing a macro and then getting to see it work the way you designed. When you are done will you please upload your code so that future CATIA users who need to write a similar macro will be able to reuse the information in this thread? Thank you.

To control the size of the newly created text use the following code. I've attached the method description from Dassault's API documentation:
Code:
drawingText1.SetFontSize 0, 0, 10.5

To control the location of the newly created text use the two x and y values (expressed in millimeters):
Code:
Set drawingText1 = drawingTexts1.Add("SCALE: " & drawingSheet1.Scale, 100, 200)
where 100 is the x location and 200 is the y location.

And here's a tip to help you find what the text location should be in your title block without having to guess and check over and over again. Simply right-click in the tool bar area while you are in the Drafting Workbench and select the "Position and Orientation" toolbar. Move your text to where you want it in the title block and then click on the text and the "Position and Orientation" toolbar will give you the text's x and y values. Convert these to millimeter values (25.4mm = 1in) and then use those values in the code above in place of "100" and "200"

What is "B SIZE SH2 FRAME.1" exactly? Is it a view that has the title block information in it?

If you do go with the method to open the template and copy and paste from it, be sure to close the template when you are done copying and pasting with the following code:
Code:
Dim documents1 As Documents
Set documents1 = CATIA.Documents

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = documents1.Open("C:\Users\JohnDoe\Desktop\Template.CATDrawing")

'copy & paste operations
'once you're done with the template then close it

drawingDocument1.Close
 
Hi drewmumaw,

Once I get the code write, I will definitely upload it!

The B SIZE SH2 FRAME.1 is the (for lack of better term, I will borrow from ACAD), the block or ditto that contains the the title block information. It's not a drawng view and doesn't show up in the drawing trea, but has it's on name. I believe it is a 2d component instance.

 
@Cbaktis
This code will select a drawing component, copy it, and then paste it on another sheet. You'll have to modify it to suit your specific script's needs. If you upload your script as you have it now I can help you do that.

Code:
Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = drawingDocument1.Selection

Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets

Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Item("Sheet.1")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Front view")

Dim drawingComponents1 As DrawingComponents
Set drawingComponents1 = drawingView1.Components

Dim drawingComponent1 As DrawingComponent
Set drawingComponent1 = drawingComponents1.Item("B SIZE SH2 FRAME.1")

selection1.Clear
selection1.Add drawingComponent1
selection1.Copy
selection1.Clear

Dim drawingSheet3 As DrawingSheet
Set drawingSheet3 = drawingSheets1.Item("Sheet.3")

drawingSheet3.Activate

selection1.Add drawingSheet3
selection1.Paste
selection1.Clear

End Sub
 
Hi drewmumaw

Attached is the code below. I've got it to run to the point of creating the new sheet and adding all the text and everything but I am struggling with the pasting operation. For some reason it stops when it gets to that script.

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim documents2 As Documents
Set documents2 = CATIA.Documents

Dim drawingDocument2 As DrawingDocument
Set drawingDocument2 = documents2.Open("I:\ENG\Chris Baktis\Catia Data\Catia Template File\B_SIZE_DRAWING.CATDrawing")

Set drawingDocument2 = CATIA.ActiveDocument

Dim selection2 As Selection
Set selection2 = drawingDocument2.Selection

Dim drawingSheets2 As DrawingSheets
Set drawingSheets2 = drawingDocument2.Sheets

Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets2.Item("2")

Dim drawingViews2 As DrawingViews
Set drawingViews2 = drawingSheet2.Views

Dim drawingView2 As DrawingView
Set drawingView2 = drawingViews2.Item("Background View")

Dim drawingComponents2 As DrawingComponents
Set drawingComponents2 = drawingView2.Components

Dim drawingComponent2 As DrawingComponent
Set drawingComponent2 = drawingComponents2.Item("B SIZE SH2 FRAME.1")

selection2.Clear
selection2.Add drawingComponent2
selection2.Copy
selection2.Clear

drawingDocument1.activate

j=drawingDocument1.Sheets.count 'counting sheets numbers for total number 

Dim oDoc As DrawingDocument 
Dim oBackGround As DrawingView 
Dim oFrontView As DrawingView 
Dim oText As DrawingText 

Set oDoc = CATIA.ActiveDocument 
Set drawingSheets1 = drawingDocument1.Sheets
Set drawingSheet1 = drawingSheets1.Add(j+1)

Dim relations1 As Relations
Set relations1 = drawingDocument1.Relations

Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim realParam1 As Parameter
Set realParam1 = parameters1.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters2 As Parameters
Set parameters2 = drawingDocument1.Parameters

Dim realParam2 As Parameter
Set realParam2 = parameters2.Item("TOTAL SHEET")

Dim parameters3 As Parameters
Set parameters3 = drawingDocument1.Parameters

Dim realParam3 As Parameter
Set realParam3 = parameters3.Item("Drawing\"&j+1&"\Alias")

Dim parameters4 As Parameters
Set parameters4 = drawingDocument1.Parameters

Dim realParam4 As Parameter
Set realParam4 = parameters4.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters5 As Parameters
Set parameters5 = drawingDocument1.Parameters

Dim realParam5 As Parameter
Set realParam5 = parameters4.Item("DRAWING NO")

realParam2.Value=j+1

Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula."&j+1, "", realParam1, "`DRAWING SCALE` ")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Background View")

drawingView1.Activate

Set oBackGround = oDoc.Sheets.ActiveSheet.Views.Item("Background View") 'select Background View 

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

Set Text = oBackGround.Texts.Add("SHEET ",401.1422, 11.43) 'write Sheet At Position 
Text.InsertVariable T,0, realParam3
iFontSize =1.524
Text.SetFontSize 0,0, iFontSize

Set oText = oBackGround.Texts.Add("SCALE",368.1476, 11.43) 'write Scale At Position 
oText.InsertVariable 0,0, realParam4
oText.SetFontSize 0,0, iFontSize

Set pText = oBackGround.Texts.Add("PARTNO",388.874,15.9258) 'write part number 
pText.InsertVariable 0,6, realParam5
pFontSize =3.048
pText.SetFontSize 0,0, pFontSize

selection2.Add drawingSheet1
selection2.Paste
selection2.Clear

End Sub
 
@Cbaktis
Delete these last three lines:
Code:
selection2.Add drawingSheet1
selection2.Paste
selection2.Clear
and replace it with:
Code:
Dim selection3 As Selection
Set selection3 = drawingDocument1.Selection
selection3.Clear
selection3.Add drawingSheet1
selection3.Paste
selection3.Clear
If you want to close the drawing file "B_SIZE_DRAWING.CATDrawing" then use this line of code at the very end right before the "End Sub" line:
Code:
drawingDocument2.Close
Here is the final code that worked on my end:
Code:
Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim documents2 As Documents
Set documents2 = CATIA.Documents

Dim drawingDocument2 As DrawingDocument
Set drawingDocument2 = documents2.Open("C:\Users\Drew\Desktop\B_SIZE_DRAWING.CATDrawing")

Set drawingDocument2 = CATIA.ActiveDocument

Dim selection2 As Selection
Set selection2 = drawingDocument2.Selection

Dim drawingSheets2 As DrawingSheets
Set drawingSheets2 = drawingDocument2.Sheets

Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets2.Item("2")

Dim drawingViews2 As DrawingViews
Set drawingViews2 = drawingSheet2.Views

Dim drawingView2 As DrawingView
Set drawingView2 = drawingViews2.Item("Background View")

Dim drawingComponents2 As DrawingComponents
Set drawingComponents2 = drawingView2.Components

Dim drawingComponent2 As DrawingComponent
Set drawingComponent2 = drawingComponents2.Item("B SIZE SH2 FRAME.1")

selection2.Clear
selection2.Add drawingComponent2
selection2.Copy
selection2.Clear

drawingDocument1.Activate

j = drawingDocument1.Sheets.Count 'counting sheets numbers for total number

Dim oDoc As DrawingDocument
Dim oBackGround As DrawingView
Dim oFrontView As DrawingView
Dim oText As DrawingText

Set oDoc = CATIA.ActiveDocument
Set drawingSheets1 = drawingDocument1.Sheets
Set drawingSheet1 = drawingSheets1.Add(j + 1)

Dim relations1 As Relations
Set relations1 = drawingDocument1.Relations

Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim realParam1 As Parameter
Set realParam1 = parameters1.Item("Drawing\" & j + 1 & "\ViewMakeUp.1\Scale")

Dim parameters2 As Parameters
Set parameters2 = drawingDocument1.Parameters

Dim realParam2 As Parameter
Set realParam2 = parameters2.Item("TOTAL SHEET")

Dim parameters3 As Parameters
Set parameters3 = drawingDocument1.Parameters

Dim realParam3 As Parameter
Set realParam3 = parameters3.Item("Drawing\" & j + 1 & "\Alias")

Dim parameters4 As Parameters
Set parameters4 = drawingDocument1.Parameters

Dim realParam4 As Parameter
Set realParam4 = parameters4.Item("Drawing\" & j + 1 & "\ViewMakeUp.1\Scale")

Dim parameters5 As Parameters
Set parameters5 = drawingDocument1.Parameters

Dim realParam5 As Parameter
Set realParam5 = parameters4.Item("DRAWING NO")

realParam2.Value = j + 1

Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula." & j + 1, "", realParam1, "`DRAWING SCALE` ")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Background View")

drawingView1.Activate

Set oBackGround = oDoc.Sheets.ActiveSheet.Views.Item("Background View") 'select Background View

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

Set Text = oBackGround.Texts.Add("SHEET ", 401.1422, 11.43) 'write Sheet At Position
Text.InsertVariable T, 0, realParam3
iFontSize = 1.524
Text.SetFontSize 0, 0, iFontSize

Set oText = oBackGround.Texts.Add("SCALE", 368.1476, 11.43) 'write Scale At Position
oText.InsertVariable 0, 0, realParam4
oText.SetFontSize 0, 0, iFontSize

Set pText = oBackGround.Texts.Add("PARTNO", 388.874, 15.9258) 'write part number
pText.InsertVariable 0, 6, realParam5
pFontSize = 3.048
pText.SetFontSize 0, 0, pFontSize

Dim selection3 As Selection
Set selection3 = drawingDocument1.Selection
selection3.Clear
selection3.Add drawingSheet1
selection3.Paste
selection3.Clear
'drawingDocument2.Close

End Sub
 
Hi drewmumaw,

Thank you for all your help! The script works perfectly but (always a but), I forgot to copy the revision block (it is a table). I thought I followed everything correctly but my script copying the table is timing out. Can you take a look?

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim documents2 As Documents
Set documents2 = CATIA.Documents

Dim drawingDocument2 As DrawingDocument
Set drawingDocument2 = documents2.Open("I:\ENG\Chris Baktis\Catia Data\Catia Template File\B_SIZE_DRAWING.CATDrawing")

Set drawingDocument2 = CATIA.ActiveDocument

Dim selection2 As Selection
Set selection2 = drawingDocument2.Selection

Dim selection4 As Selection
Set selection4 = drawingDocument2.Selection

Dim drawingSheets2 As DrawingSheets
Set drawingSheets2 = drawingDocument2.Sheets

Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets2.Item("2")

Dim drawingViews2 As DrawingViews
Set drawingViews2 = drawingSheet2.Views

Dim drawingView2 As DrawingView
Set drawingView2 = drawingViews2.Item("Background View")

Dim drawingComponents2 As DrawingComponents
Set drawingComponents2 = drawingView2.Components

Dim drawingComponent2 As DrawingComponent
Set drawingComponent2 = drawingComponents2.Item("B SIZE SH2 FRAME.1")

selection2.Clear
selection2.Add drawingComponent2
selection2.Copy
selection2.Clear

Dim DrawingTables1 as DrawingTables
Set DrawingTables1 = drawingView2.Tables

Dim drawingTable1 As DrawingTable
Set drawingTable1 = DrawingTables1.Item("Table.1")

selection4.Clear
selection4.Add drawingTable1
selection4.Copy
selection4.Clear

drawingDocument1.activate

j=drawingDocument1.Sheets.count 'counting sheets numbers for total number 

Dim oDoc As DrawingDocument 
Dim oBackGround As DrawingView 
Dim oFrontView As DrawingView 
Dim oText As DrawingText 

Set oDoc = CATIA.ActiveDocument 
Set drawingSheets1 = drawingDocument1.Sheets
Set drawingSheet1 = drawingSheets1.Add(j+1)

Dim relations1 As Relations
Set relations1 = drawingDocument1.Relations

Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim realParam1 As Parameter
Set realParam1 = parameters1.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters2 As Parameters
Set parameters2 = drawingDocument1.Parameters

Dim realParam2 As Parameter
Set realParam2 = parameters2.Item("TOTAL SHEET")

Dim parameters3 As Parameters
Set parameters3 = drawingDocument1.Parameters

Dim realParam3 As Parameter
Set realParam3 = parameters3.Item("Drawing\"&j+1&"\Alias")

Dim parameters4 As Parameters
Set parameters4 = drawingDocument1.Parameters

Dim realParam4 As Parameter
Set realParam4 = parameters4.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters5 As Parameters
Set parameters5 = drawingDocument1.Parameters

Dim realParam5 As Parameter
Set realParam5 = parameters4.Item("DRAWING NO")

realParam2.Value=j+1

Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula."&j+1, "", realParam1, "`DRAWING SCALE` ")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Background View")

drawingView1.Activate

Set oBackGround = oDoc.Sheets.ActiveSheet.Views.Item("Background View") 'select Background View 

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

Set Text = oBackGround.Texts.Add("      ",401.1422, 11.43) 'write Sheet At Position 
Text.InsertVariable 0,0, realParam3
iFontSize =1.524
Text.SetFontSize 0,0, iFontSize
Text.AnchorPosition=catMiddleLeft

Set oText = oBackGround.Texts.Add("SCALE",368.1476, 11.43) 'write Scale At Position 
oText.InsertVariable 0,5, realParam4
oText.SetFontSize 0,0, iFontSize
oText.AnchorPosition=catMiddleLeft


Set pText = oBackGround.Texts.Add("PARTNO",388.874,15.9258) 'write part number 
pText.InsertVariable 0,6, realParam5
pFontSize =3.048
pText.SetFontSize 0,0, pFontSize
pText.AnchorPosition=catMiddleLeft

Dim selection3 As Selection
Set selection3 = drawingDocument1.Selection
selection3.Clear
selection3.Add drawingSheet1
selection3.Paste
selection3.Clear 

Dim selection5 As Selection
Set selection5 = drawingDocument2.Selection
selection5.Clear
selection5.Add drawingSheet1
selection5.Paste
selection5.Clear 



drawingDocument2.Close 

End Sub
 
@Cbaktis
That's no problem, we can just add "Table.1" to the original "selection2" before the copy operation. The following code worked on my end:
Code:
Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim documents2 As Documents
Set documents2 = CATIA.Documents

Dim drawingDocument2 As DrawingDocument
Set drawingDocument2 = documents2.Open("C:\Users\Drew\Desktop\B_SIZE_DRAWING.CATDrawing")

Set drawingDocument2 = CATIA.ActiveDocument

Dim selection2 As Selection
Set selection2 = drawingDocument2.Selection

Dim drawingSheets2 As DrawingSheets
Set drawingSheets2 = drawingDocument2.Sheets

Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets2.Item("2")

Dim drawingViews2 As DrawingViews
Set drawingViews2 = drawingSheet2.Views

Dim drawingView2 As DrawingView
Set drawingView2 = drawingViews2.Item("Background View")

Dim drawingTables1 As DrawingTables
Set drawingTables1 = drawingView2.Tables

Dim drawingTable1 As DrawingTable
Set drawingTable1 = drawingTables1.GetItem("Table.1")

Dim drawingComponents2 As DrawingComponents
Set drawingComponents2 = drawingView2.Components

Dim drawingComponent2 As DrawingComponent
Set drawingComponent2 = drawingComponents2.Item("B SIZE SH2 FRAME.1")

selection2.Clear
selection2.Add drawingComponent2
selection2.Add drawingTable1
selection2.Copy
selection2.Clear

drawingDocument1.Activate

j = drawingDocument1.Sheets.Count 'counting sheets numbers for total number

Dim oDoc As DrawingDocument
Dim oBackGround As DrawingView
Dim oFrontView As DrawingView
Dim oText As DrawingText

Set oDoc = CATIA.ActiveDocument
Set drawingSheets1 = drawingDocument1.Sheets
Set drawingSheet1 = drawingSheets1.Add(j + 1)

Dim relations1 As Relations
Set relations1 = drawingDocument1.Relations

Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim realParam1 As Parameter
Set realParam1 = parameters1.Item("Drawing\" & j + 1 & "\ViewMakeUp.1\Scale")

Dim parameters2 As Parameters
Set parameters2 = drawingDocument1.Parameters

Dim realParam2 As Parameter
Set realParam2 = parameters2.Item("TOTAL SHEET")

Dim parameters3 As Parameters
Set parameters3 = drawingDocument1.Parameters

Dim realParam3 As Parameter
Set realParam3 = parameters3.Item("Drawing\" & j + 1 & "\Alias")

Dim parameters4 As Parameters
Set parameters4 = drawingDocument1.Parameters

Dim realParam4 As Parameter
Set realParam4 = parameters4.Item("Drawing\" & j + 1 & "\ViewMakeUp.1\Scale")

Dim parameters5 As Parameters
Set parameters5 = drawingDocument1.Parameters

Dim realParam5 As Parameter
Set realParam5 = parameters4.Item("DRAWING NO")

realParam2.Value = j + 1

Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula." & j + 1, "", realParam1, "`DRAWING SCALE` ")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Background View")

drawingView1.Activate

Set oBackGround = oDoc.Sheets.ActiveSheet.Views.Item("Background View") 'select Background View

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

Set Text = oBackGround.Texts.Add("SHEET ", 401.1422, 11.43) 'write Sheet At Position
Text.InsertVariable T, 0, realParam3
iFontSize = 1.524
Text.SetFontSize 0, 0, iFontSize

Set oText = oBackGround.Texts.Add("SCALE", 368.1476, 11.43) 'write Scale At Position
oText.InsertVariable 0, 0, realParam4
oText.SetFontSize 0, 0, iFontSize

Set pText = oBackGround.Texts.Add("PARTNO", 388.874, 15.9258) 'write part number
pText.InsertVariable 0, 6, realParam5
pFontSize = 3.048
pText.SetFontSize 0, 0, pFontSize

Dim selection3 As Selection
Set selection3 = drawingDocument1.Selection
selection3.Clear
selection3.Add drawingSheet1
selection3.Paste
selection3.Clear

drawingDocument2.Close

End Sub
 
Hi drewmumaw,

I see where I went wrong with my script for the table. I had drawingTables1.Item("Table.1")
as opposed to drawingTables1.GetItem("Table.1")!

Here is the final script, I modified it to remove and extra line that wasn't doing anything and then added script to return to the Main View!

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim documents2 As Documents
Set documents2 = CATIA.Documents

Dim drawingDocument2 As DrawingDocument
Set drawingDocument2 = documents2.Open("I:\ENG\Chris Baktis\Catia Data\Catia Template File\B_SIZE_DRAWING.CATDrawing")

Set drawingDocument2 = CATIA.ActiveDocument

Dim selection2 As Selection
Set selection2 = drawingDocument2.Selection

Dim drawingSheets2 As DrawingSheets
Set drawingSheets2 = drawingDocument2.Sheets

Dim drawingSheet2 As DrawingSheet
Set drawingSheet2 = drawingSheets2.Item("2")

Dim drawingViews2 As DrawingViews
Set drawingViews2 = drawingSheet2.Views

Dim drawingView2 As DrawingView
Set drawingView2 = drawingViews2.Item("Background View")

Dim drawingTables1 As DrawingTables
Set drawingTables1 = drawingView2.Tables

Dim drawingTable1 As DrawingTable
Set drawingTable1 = drawingTables1.GetItem("Table.1")

Dim drawingComponents2 As DrawingComponents
Set drawingComponents2 = drawingView2.Components

Dim drawingComponent2 As DrawingComponent
Set drawingComponent2 = drawingComponents2.Item("B SIZE SH2 FRAME.1")

selection2.Clear
selection2.Add drawingComponent2
selection2.Add drawingTable1
selection2.Copy
selection2.Clear

drawingDocument1.activate

j=drawingDocument1.Sheets.count 'counting sheets numbers for total number 

Dim oDoc As DrawingDocument 
Dim oBackGround As DrawingView 
Dim oFrontView As DrawingView 
Dim oText As DrawingText 

Set oDoc = CATIA.ActiveDocument 
Set drawingSheets1 = drawingDocument1.Sheets
Set drawingSheet1 = drawingSheets1.Add(j+1)

Dim relations1 As Relations
Set relations1 = drawingDocument1.Relations

Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters

Dim realParam1 As Parameter
Set realParam1 = parameters1.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters2 As Parameters
Set parameters2 = drawingDocument1.Parameters

Dim realParam2 As Parameter
Set realParam2 = parameters2.Item("TOTAL SHEET")

Dim parameters3 As Parameters
Set parameters3 = drawingDocument1.Parameters

Dim realParam3 As Parameter
Set realParam3 = parameters3.Item("Drawing\"&j+1&"\Alias")

Dim parameters4 As Parameters
Set parameters4 = drawingDocument1.Parameters

Dim realParam4 As Parameter
Set realParam4 = parameters4.Item("Drawing\"&j+1&"\ViewMakeUp.1\Scale")

Dim parameters5 As Parameters
Set parameters5 = drawingDocument1.Parameters

Dim realParam5 As Parameter
Set realParam5 = parameters4.Item("DRAWING NO")

realParam2.Value=j+1

Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula."&j+1, "", realParam1, "`DRAWING SCALE` ")

Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views

Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Background View")

drawingView1.Activate

Set oBackGround = oDoc.Sheets.ActiveSheet.Views.Item("Background View") 'select Background View 

Dim drawingRoot1 As DrawingRoot
Set drawingRoot1 = drawingDocument1.GetItem("CATIADrawingDrawing1")

Set Text = oBackGround.Texts.Add("      ",401.1422, 11.43) 'write Sheet At Position 
Text.InsertVariable 0,0, realParam3
iFontSize =1.524
Text.SetFontSize 0,0, iFontSize
Text.AnchorPosition=catMiddleLeft

Set oText = oBackGround.Texts.Add("SCALE",368.1476, 11.43) 'write Scale At Position 
oText.InsertVariable 0,5, realParam4
oText.SetFontSize 0,0, iFontSize
oText.AnchorPosition=catMiddleLeft


Set pText = oBackGround.Texts.Add("PARTNO",388.874,15.9258) 'write part number 
pText.InsertVariable 0,6, realParam5
pFontSize =3.048
pText.SetFontSize 0,0, pFontSize
pText.AnchorPosition=catMiddleLeft

Dim selection3 As Selection
Set selection3 = drawingDocument1.Selection
selection3.Clear
selection3.Add drawingSheet1
selection3.Paste
selection3.Clear 

Set drawingView1 = drawingViews1.Item("Main View")

drawingView1.Activate

drawingDocument2.Close 

End Sub
 
Now on to the next 2 tasks!

1. Creating a macro to modify page set up, add the parameters, and then put sheet 1 in if some doesn't start from the Start template!
2. Liinking Model Weights to the drawing!
 
Hi Fernando,

I've glanced at it quickly but I think that will help. One thing I am strugling with though is the scales of the drawings. I thought I had it working in my previous script and it's not.

The one thing I would like to do set all drawing sheets equal to the same scale so that if when importing views they all come in the same scale.

I understand that the sheet scales are controlled by a Global Scale, but what I can see to find is the link between the Global Scale and the Sheet Scales, I believe it is hidden deep inside the drawing and struggling for a way to do this.

What I would optimally like to do is use the parameter that I have created and use this to update all sheet and all views when a user changes the value. I know that this maybe possible with a script but is there a way to trigger the script when the user changes the global parameters.

Can I possibly do this with formula instead, but I guess that would be a matter of finding where the sheetscale and globalscales are hidden.

Any idea on this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top