VBA to ask for Titleborders

VBA to ask for Titleborders

UBNTI
Advocate Advocate
1,880 Views
12 Replies
Message 1 of 13

VBA to ask for Titleborders

UBNTI
Advocate
Advocate

Hi,

 

I like to use VBA to access the Titleborders in Modespace. 

First action should count the Titleborders and second should export each Titleborder as a PDF.

Is this possible?

 

Additionally I like to ask some Properties from the TitleBlock.

 

I have done a lot of VBA in Inventor, but not in Autocad. 

Thank you for helping me!

 

UB

0 Likes
1,881 Views
12 Replies
Replies (12)
Message 2 of 13

pendean
Community Legend
Community Legend
Message 3 of 13

Ed__Jobe
Mentor
Mentor

Hello @UBNTI This has been tried before, but there isn't a clean way to distinguish one print area from another. The problem lies in the fact that you shouldn't be putting sheet entities in model space, which is just for your physical model. The title block belongs in paper space. Then there are built-in functions to do what you want.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 13

grobnik
Collaborator
Collaborator

Hi @UBNTI ,

Concerning the print area, @Ed__Jobe already answered to you, concerning title border in ModelSpace, If I understand well, you would like to manage mainly a block or more than a block with attribute, because title border it's a block and for doing this trough VBA there are several method.

So, first of all I'm demonstrating you that the drawing title border it's a block.

  • If you Click from Autocad menu Annotate tab ->Sheet panel -> Title Border and select for example the ISO A3 you will insert at least two blocks the first one is called ISO_A3, and the second one is called ISO_TITLEA where to store drawing info

grobnik_0-1607457730030.png

Now we will see how to have access to these blocks by VBA, and later we will see how to manage PDF:

once you create a block with attributes you can use the code below to find your block with a wanted attribute name.

 

Sub ChangeTitleBlock()
For Each Block In ThisDrawing.ModelSpace
    If TypeOf Block Is AcadBlockReference Then
        If Block.Name = "ISO_TITLEA" Then ' Insert here block name
            If Block.HasAttributes = True Then
                Set MyBlock = Block
                MyAtt = MyBlock.GetAttributes
                For A = LBound(MyAtt) To UBound(MyAtt)
                    If MyAtt(A).TagString = "GEN-TITLE-DWG{13.6}" Then ' Name of attribute
                        NEW_TITLE = "NEW DRAWING NAME"
                        MyAtt(A).TextString = NEW_TITLE 
                    End If
                Next
            End If
        End If
    End If
Next
ThisDrawing.Regen acAllViewports
End Sub

 

When you create a block with attributes inside a drawing or template,  later by VBA you will have an array coming from BlockObject.GetAttributes function.

Each attribute will have TextString and TagString properties, TAGSTRING it's the "NAME" of Attribute, and TextString it's the content, so you can check inside all BlockObject attributes value the Name, and change the value.

Into above code I'll change the first attribute related to title drawing, stored in array (0) and called "GEN-TITLE-DWG{13.6}"

I hope how above should solve partially your issue, and if I not understand well your request please let me know.

Later I'll show you how to export to PDF.

Bye

Message 5 of 13

UBNTI
Advocate
Advocate

Thank you for the part. It is interessting. And it works. Thank you for the rest with the PDF 🙂

 

0 Likes
Message 6 of 13

grobnik
Collaborator
Collaborator

Hi @UBNTI ,

thank you for message, concerning the pdf part, I would like to understand better you request indicated in the starting message with "....each Titleborder as a PDF...." what do you mean with each, I guess you have only one titleblock in model space working area, even if usually titleblock are placed in Layout  working area.

Could you share an example drawing in order to understand better you request ?.

Of course nobody would like to have access to potential high importance info inside the drawing it's a test.

As second issue, for other forum member searching option could you mark as accepted solution my post ?, even if I guess it solved partially your issue.

Thank you.

0 Likes
Message 7 of 13

UBNTI
Advocate
Advocate

Hi and thank you for the answer. But it is really true that htere are more than one Borderframes in the Modelspace of a DWG and each Borderframe shoulb be a own PDF.

And bestway the PDF should be named with Properties from its Titleblock 🙂

Thanks again.

ub_0-1608037997631.png

 

0 Likes
Message 8 of 13

grobnik
Collaborator
Collaborator

Ok I understand, but with-out knowing the print area or title block dimensions and coordinates inside the model space workspace it's really impossible to print separately each one.

I'm suggesting to create a single PaperSpace viewport for each title block, I guess you know how to made.

May you share the drawing ? perphaps only the title block with-out contents.

In any case I'll try to find a solution, again the best should be share the drawing, but for the next drwing you will face off the same issue due to the title block dimanesions could be different, so create a standard procedure it's not so easy.

Bye.

0 Likes
Message 9 of 13

UBNTI
Advocate
Advocate

Hi, 

I have a copy of the drawing here.

0 Likes
Message 10 of 13

maratovich
Advisor
Advisor

@UBNTI wrote:

Hi and thank you for the answer. But it is really true that htere are more than one Borderframes in the Modelspace of a DWG and each Borderframe shoulb be a own PDF.

And bestway the PDF should be named with Properties from its Titleblock 🙂

Thanks again.

 


It is possible. No problem. Use this - Revers 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 11 of 13

maratovich
Advisor
Advisor

What do you want to name the print files?

What attribute should you get from the stamp?

 

 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 12 of 13

grobnik
Collaborator
Collaborator

Hi @UBNTI 

here a code, it's not yet definitive due to I'm able to print only one titleborder, I'm working on.

Of course pagesetup shall be fixed with your plot style and drawing scale.

I'm thinking to select a plot window by VBA, and print title border, it's under test.

Let me know

Sub CreatePDF()

Dim PtConfigs As AcadPlotConfigurations
Dim PlotConfig As AcadPlotConfiguration
Dim PtObj As AcadPlot
Dim BackPlot As Variant

'Create a new plot configutrarion with all needed parameters
Set PtObj = ThisDrawing.Plot
Set PtConfigs = ThisDrawing.PlotConfigurations
'Add a new plot configuration
PtConfigs.Add "PDF", False
'The plot config you created become active
Set PlotConfig = PtConfigs.Item("PDF")
'Use this method to set the scale
'PlotConfig.StandardScale = acScaleToFit
PlotConfig.StandardScale = acScaleToFit

'Updates the plot
PlotConfig.RefreshPlotDeviceInfo
'Here you specify the pc3 file you want to use
PlotConfig.ConfigName = "DWG To PDF.pc3"
'You can select the plot style table here
PlotConfig.StyleSheet = "Acad.ctb"
'Specifies whether or not to plot using the plot styles
PlotConfig.PlotWithPlotStyles = True

'If you are going to create pdf files in a batch mode,
'I would recommend to turn off the BACKGROUNDPLOT system variable,
'so autocad will not continue to do anything until finishes
'the pdf creation
BackPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
'Updates the plot
PlotConfig.RefreshPlotDeviceInfo
'Now you can use the PlotTofile method
If PtObj.PlotToFile(Replace(ThisDrawing.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
MsgBox "PDF Was Created"
Else
MsgBox "PDF Creation Unsuccessful"
End If
'If you wish you can delete th plot configuration you created
'programmatically, and set the 'BACKGROUNDPLOT' system variable
'to its original status.
PtConfigs.Item("PDF").Delete
Set PlotConfig = Nothing
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot

End Sub

 

0 Likes
Message 13 of 13

m_latz
Advisor
Advisor

@UBNTI , just another maybe helpful information:

 

I've seen in your sample drawing, that it is an AutoCAD Mechnical drawing with a "mechanical drawing border".

 

Do you know, that there is a additional API for Mechanical available, where you have special functions to get all drawing border, drawing title blocks, edit attributes, etc, ...

 

Here you can download the SDK for the different AutoCAD Mechanical version:

AutoCAD Mechanical Platform Technologies | Autodesk Developer Network

 

You should download the version for your AutoCAD Mechanical and also AcadM 2018 SDK, because there is an additional help file for VBA use.

AcadM-API.png

I have been using the AcadM SDK (C++) for several years and it is so much easier with.

 

regards

 

Markus

0 Likes