Macro To Add Additional Sheets to a Drawing
Macro To Add Additional Sheets to a Drawing
(OP)
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.
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.





RE: Macro To Add Additional Sheets to a Drawing
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!
RE: Macro To Add Additional Sheets to a Drawing
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 --> vba
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 SubRE: Macro To Add Additional Sheets to a Drawing
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!
RE: Macro To Add Additional Sheets to a Drawing
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 --> vba
Dim parameters1 As Parameters Set parameters1 = drawingDocument1.Parameters Dim intParam1 As IntParam Set intParam1 = parameters1.Item("TOTAL SHEET") intParam1.Value = drawingSheets1.CountRE: Macro To Add Additional Sheets to a Drawing
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 --> vba
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)RE: Macro To Add Additional Sheets to a Drawing
CODE --> vba
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.
RE: Macro To Add Additional Sheets to a Drawing
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 --> vba
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 SubRE: Macro To Add Additional Sheets to a Drawing
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.
RE: Macro To Add Additional Sheets to a Drawing
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 --> vba
To control the location of the newly created text use the two x and y values (expressed in millimeters):
CODE --> vba
Set drawingText1 = drawingTexts1.Add("SCALE: " & drawingSheet1.Scale, 100, 200)http://files.engineering.com/getfile.aspx?folder=d...
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"
http://files.engineering.com/getfile.aspx?folder=4...
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 --> vba
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.CloseRE: Macro To Add Additional Sheets to a Drawing
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.
RE: Macro To Add Additional Sheets to a Drawing
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 --> vba
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 SubRE: Macro To Add Additional Sheets to a Drawing
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 SubRE: Macro To Add Additional Sheets to a Drawing
Delete these last three lines:
CODE --> vba
CODE --> vba
CODE --> vba
CODE --> vba
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 SubRE: Macro To Add Additional Sheets to a Drawing
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 SubRE: Macro To Add Additional Sheets to a Drawing
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 --> vba
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 SubRE: Macro To Add Additional Sheets to a Drawing
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 SubRE: Macro To Add Additional Sheets to a Drawing
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!
RE: Macro To Add Additional Sheets to a Drawing
Maybe this will help you a little bit
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Macro To Add Additional Sheets to a Drawing
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?
RE: Macro To Add Additional Sheets to a Drawing
If you want the scale of sheets to change when a parameter value changes, the best way is to use KWA logic. More specifically, via a "reaction" that gets triggered whenever the parameter value changes. You will need a KWA license.
As far as the code itself for changing the scale of drawing sheets, it's just:
CODE --> vba
Dim drawingDocument1 As DrawingDocument Set drawingDocument1 = CATIA.ActiveDocument Dim drawingSheets1 As DrawingSheets Set drawingSheets1 = drawingDocument1.Sheets Dim drawingSheet1 As DrawingSheet Set drawingSheet1 = drawingSheets1.Item("Sheet.1") drawingSheet1.Scale2 = 0.5To be able to search for some of this code within the API documentation yourself, I suggest opening up CATIA V5's VBA Editor under Tools > Macro > Visual Basic Editor... or Alt+F11. You can then right click on the .catvba tool in the "Project" window on the left hand side and click Insert > Module then type in Sub CATMain () and End Sub. In between select Insert > Object resolution and then click the sheet in the tree. In the vba editor you can type in the object and then type "." to see what properties and methods are available via the API for that object (see step 4 in attached image). For example type "drawingSheet1.Scale" or "drawingSheet1.PageSetup". Put your cursor in the text "PageSetup" and hit F1 to bring up the API documentation (also attached).
http://files.engineering.com/getfile.aspx?folder=2...
http://files.engineering.com/getfile.aspx?folder=f...
That should get you started...
RE: Macro To Add Additional Sheets to a Drawing
Thank you for the tip. I was able to write the code no problem. I've looked and I don't think we have a KWA license (under tools->general->Licenses KWA says "no license"). Do you know any other options to have it autorun without needing the KWA license?
RE: Macro To Add Additional Sheets to a Drawing
The other option would be to use a relation to link the view scales directly to the parameter. Screen grabs to come on my next post.
RE: Macro To Add Additional Sheets to a Drawing
http://files.engineering.com/getfile.aspx?folder=5...
Step2: click the real parameter from the tree that you want the view scale linked to.
http://files.engineering.com/getfile.aspx?folder=7...
Now you should be able to change the parameter in the tree and see the view scale up and down accordingly.
RE: Macro To Add Additional Sheets to a Drawing
Were you able to successfully control the view scales via a script?
Drew Mumaw
http://www.drewmumaw.com/
http://www.textsketcher.com/
RE: Macro To Add Additional Sheets to a Drawing
It is a real pleasure to see how this thread is going on, I should put a star somewhere for both of you, I'll do it now, is a nice example what can be done when peoples are talking.
@ drewmumaw - useful application for those who need, much better then others dealing with 3D text (from what I saw up to now).
Code bellow is adding a drawing view at a specific scale, 2:1 (maybe will help Cbaktis a little bit).
CODE --> CATScript
Language="VBSCRIPT" Sub CATMain() Dim drawingDocument1 As Document Set drawingDocument1 = CATIA.ActiveDocument Dim drawingSheets1 As DrawingSheets Set drawingSheets1 = drawingDocument1.Sheets Dim drawingSheet1 As DrawingSheet Set drawingSheet1 = drawingSheets1.ActiveSheet Dim drawingViews1 As DrawingViews Set drawingViews1 = drawingSheet1.Views Dim drawingView1 As DrawingView Set drawingView1 = drawingViews1.Add("View1") Dim drawingViewGenerativeBehavior1 As DrawingViewGenerativeBehavior Set drawingViewGenerativeBehavior1 = drawingView1.GenerativeBehavior drawingViewGenerativeBehavior1.DefineFrontView 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000 drawingView1.x = 100 drawingView1.y = 400 Dim double1 As Double double1 = drawingSheet1.Scale drawingView1.Scale = 2.000000 drawingView1.Activate Dim specsAndGeomWindow1 As Window Set specsAndGeomWindow1 = CATIA.ActiveWindow Dim specsViewer1 As Viewer Set specsViewer1 = specsAndGeomWindow1.ActiveViewer specsViewer1.Reframe End SubRegards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Macro To Add Additional Sheets to a Drawing
Thank you for the code and comment.
Regards,
Drew Mumaw
http://www.drewmumaw.com/
http://www.textsketcher.com/
RE: Macro To Add Additional Sheets to a Drawing
CODE -->
Thank you for the code and comments as well!
drewmumaw,
Sorry, I was away for the weekend! I did get scale script working, what we do here is use the same scale on all sheets of the drawing so I wrote a simple script which updates all the scales in the drawing to the user designated "DRAWING SCALE". The only issue I am having which is more a nuisance than anything else is that the start template that I created has a formual which basicalls sets '1\viewmakeup.1\scale'='DRAWING SCALE' and the same for each sheet is that when I change the DRAWING SCALE parameter it changes the shown scale to the same number. When I run the script, it sets the sheet scale to the right value but for some reason on sheet 1 and 2 (which were included in the start template) it doubles the scale (meaning 1/2 would become 1/4) that is shown on the f/d, but all views and drawing sheets scale to the correct scale. I will probably rework how I get the scale to show on the drawing and try to eliminate the formulas. The only other issue that I have is that since we don't have a KWA license is a way of deploying the scale change script automatically. The user feed back here is that they don't want to have to run a macro everytime to scale the drawing (thought it is much simplere than having to scale a lot of views). What I may do is create a GUI interface to deploy all the drawng related scripts in a menu set up and create a custom tool bar button with the the GUI load to that, so that in the drafting work bench users can launch the drawing scripts menu from there!
Here is the script that I have written:
CODE -->
Sub CATMain() Dim drawingDocument1 As DrawingDocument Set drawingDocument1 = CATIA.ActiveDocument Dim parameters1 As Parameters Set parameters1 = drawingDocument1.Parameters Dim drawingSheets1 As DrawingSheets Set drawingSheets1 = drawingDocument1.Sheets j = drawingDocument1.Sheets.Count Dim realParam1 As realParam Set realParam1 = parameters1.Item("DRAWING SCALE") For i = 1 to j Dim drawingSheet1 As DrawingSheet Set drawingSheet1 = drawingSheets1.Item(i) drawingSheet1.Scale = realParam1.value next End SubRE: Macro To Add Additional Sheets to a Drawing
Many companies deploy their macros in the way you described - by creating a custom toolbar. That sounds like a good idea.
With regards to Sheet.1 and Sheet.2 not scaling properly, a quick fix would be to just modify your loop at the end of your script to check if it's Sheet.1 or Sheet.2 and scale those sheets slightly differently in relation to your parameter DRAWING SCALE. The code would look like this:
CODE --> vba
For i = 1 To j Dim drawingSheet1 As DrawingSheet Set drawingSheet1 = drawingSheets1.Item(i) If drawingSheet1.Name = "Sheet.1" Or drawingSheet1.Name = "Sheet.2" Then drawingSheet1.Scale2 = realParam1.Value * 2 Else drawingSheet1.Scale2 = realParam1.Value End If NextRegards,
Drew Mumaw
http://www.drewmumaw.com/
http://www.textsketcher.com/