|
| 1 | +--- |
| 2 | +title: Getting Started with AutoCAD VBA 5 : Set Colors, Layers, Text Style, LineTypes |
| 3 | +description : learn to modify AutoCAD object properties |
| 4 | +date: 01-02-2025 |
| 5 | +categories: [VBA, AutoCAD] |
| 6 | +tag: [autocad, vba,howto] |
| 7 | +image: /assets/images/autocad/autocad-getting-started.webp |
| 8 | +--- |
| 9 | + |
| 10 | +### Overview |
| 11 | +- In this tutorial I’ll show you how to use VBA to add hatch to your drawings |
| 12 | +- I am assuming that |
| 13 | + - you've already installed [AutoCAD VBA Module](https://www.autodesk.com/support/technical/article/caas/tsarticles/ts/3kxk0RyvfWTfSfAIrcmsLQ.html) |
| 14 | + - you have basic knowledge of `VBA` and how to create new method or functions |
| 15 | + - you already know how to draw basic objects , if not please go through this post first : [Getting Started with AutoCAD VBA 1 : Line, Polyline, Circle, Arc, Rectangle, Point](/posts/autocad-vba-getting-started-1/) |
| 16 | + |
| 17 | +### Setup on AutoCAD |
| 18 | +- Open blank AutoCAD file with default template, open Visual Basic Editor and Add new module |
| 19 | +- Add any sample Code from below and just run it, try to change values like colors, lineTypes, Fonts and re-run it. |
| 20 | +- Sample codes for each basic objects are given below. You can copy paste this code to `VBA` editor to directly run it without any inputs |
| 21 | +- Current code is very simple, I'll try to add bit more details into this code in future, like code to modify it's different properties |
| 22 | +- This is very basic code and self-explanatory, if you still need help then use AI tools like ChatGPT to understand this code, only contact me if everything else fail 😅 |
| 23 | + |
| 24 | +### Set Color for AutoCAD Objects |
| 25 | +- This code should work with almost all autocad objects |
| 26 | +- We are going to use circle object for this example, since it require least amout of code but you can use any object |
| 27 | + |
| 28 | +```vb |
| 29 | +Sub DrawCircle() |
| 30 | + |
| 31 | + 'Circle center x,y,z coordinate |
| 32 | + Dim centerPoint(0 To 2) As Double |
| 33 | + centerPoint(0) = 10#: centerPoint(1) = 20#: centerPoint(2) = 0# |
| 34 | + |
| 35 | + 'Circle radius |
| 36 | + Dim radius As Double |
| 37 | + radius = 10# |
| 38 | + |
| 39 | + 'Create circle object |
| 40 | + Dim cadCircle As AcadCircle |
| 41 | + Set cadCircle = ThisDrawing.ModelSpace.AddCircle(centerPoint, radius) |
| 42 | + |
| 43 | + 'Change Circle color using color name |
| 44 | + 'acRed is autocad inbuilt varible part of AcColor Enum |
| 45 | + cadCircle.color = acRed |
| 46 | + |
| 47 | + 'Red color (Color index 1) |
| 48 | + cadCircle.color = 1 |
| 49 | + |
| 50 | +End Sub |
| 51 | +``` |
| 52 | +- You can specifiy colors in autocad using two method |
| 53 | + - Using AutoCAD internal Variables AcColor |
| 54 | + - You can either specify color name or use layer color |
| 55 | + - acByLayer will automatically display entity in layor color |
| 56 | + - Color Index : predefine value of color in integer |
| 57 | + |
| 58 | +| Color Variable | Value | |
| 59 | +| -------------- | ----- | |
| 60 | +| acByBlock | 0 | |
| 61 | +| acRed | 1 | |
| 62 | +| acYellow | 2 | |
| 63 | +| acGreen | 3 | |
| 64 | +| acCyan | 4 | |
| 65 | +| acBlue | 5 | |
| 66 | +| acMagenta | 6 | |
| 67 | +| acWhite | 7 | |
| 68 | +| acDarkGray | 8 | |
| 69 | +| acLightGray | 9 | |
| 70 | +| acByLayer | 256 | |
| 71 | + |
| 72 | + |
| 73 | +_Screenshot 1 : AutoCAD Color Picker_ |
| 74 | + |
| 75 | +### Set Layer for AutoCAD Objects |
| 76 | +```vb |
| 77 | +Sub CreateLayer() |
| 78 | + |
| 79 | + Dim layerName As String |
| 80 | + layerName = "Reinforcement" |
| 81 | + |
| 82 | + Dim layerColor As Integer |
| 83 | + ' Red color (Color index 1) |
| 84 | + layerColor = 1 |
| 85 | + |
| 86 | + ' Check if the layer already exists$$ |
| 87 | + Dim layer As AcadLayer |
| 88 | + On Error Resume Next |
| 89 | + Set layer = ThisDrawing.Layers(layerName) |
| 90 | + On Error GoTo 0 |
| 91 | + |
| 92 | + ' If the layer does not exist, create it |
| 93 | + If layer Is Nothing Then |
| 94 | + Set layer = ThisDrawing.Layers.Add(layerName) |
| 95 | + layer.color = layerColor |
| 96 | + MsgBox "Layer '" & layerName & "' created successfully with red color.", vbInformation |
| 97 | + Else |
| 98 | + MsgBox "Layer '" & layerName & "' already exists.", vbExclamation |
| 99 | + End If |
| 100 | + |
| 101 | +End Sub |
| 102 | +``` |
| 103 | +### Set LineSyle for AutoCAD Objects |
| 104 | + |
| 105 | +### Set TextStyle for AutoCAD Objects |
0 commit comments