A request came in by email during last week’s vacation:
I have been looking around to find a way about creating Dimension Style Overrides, but have not really had any success at anything yet. I have created program in which I do create several dimension styles, but I just keep getting lost with the overrides.
This seemed like a really good topic to cover, so this post contains some simple code that to create a dimension style and two nearly-identical linear dimensions: both use our newly-created dimension style but the second of the two also contains some Dimension Style Overrides, which we attach to the dimension via Extended Entity Data (XData).
Here’s the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace DimStyleOverrideTest
{
public class Commands
{
[CommandMethod("ODS")]
public void OverrideDimensionStyle()
{
Database db =
HostApplicationServices.WorkingDatabase;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
// Open our dimension style table to add our
// new dimension style
DimStyleTable dst =
(DimStyleTable)tr.GetObject(
db.DimStyleTableId, OpenMode.ForWrite
);
// Create our new dimension style
DimStyleTableRecord dstr =
new DimStyleTableRecord();
dstr.Dimtad = 2;
dstr.Dimgap = 0.3;
dstr.Name = "MyStyle";
// Add it to the dimension style table
ObjectId dsId = dst.Add(dstr);
tr.AddNewlyCreatedDBObject(dstr, true);
// Now create two identical dimensions, one
// next to the other, using our dimension
// style
AlignedDimension ad1 =
new AlignedDimension(
Point3d.Origin,
new Point3d(5.0, 0.0, 0.0),
new Point3d(2.5, 2.0, 0.0),
"Standard dimension",
dsId
);
// The only thing we change is the text string
AlignedDimension ad2 =
new AlignedDimension(
new Point3d(5.0, 0.0, 0.0),
new Point3d(10.0, 0.0, 0.0),
new Point3d(7.5, 2.0, 0.0),
"Overridden dimension",
dsId
);
/*
Now we'll add dimension overrides for DIMTAD
and DIMGAP via XData
Dimension variable group codes are:
DIMPOST 3
DIMAPOST 4
DIMSCALE 40
DIMASZ 41
DIMEXO 42
DIMDLI 43
DIMEXE 44
DIMRND 45
DIMDLE 46
DIMTP 47
DIMTM 48
DIMTOL 71
DIMLIM 72
DIMTIH 73
DIMTOH 74
DIMSE1 75
DIMSE2 76
DIMTAD 77
DIMZIN 78
DIMAZIN 79
DIMTXT 140
DIMCEN 141
DIMTSZ 142
DIMALTF 143
DIMLFAC 144
DIMTVP 145
DIMTFAC 146
DIMGAP 147
DIMALTRND 148
DIMALT 170
DIMALTD 171
DIMTOFL 172
DIMSAH 173
DIMTIX 174
DIMSOXD 175
DIMCLRD 176
DIMCLRE 177
DIMCLRT 178
DIMADEC 179
DIMDEC 271
DIMTDEC 272
DIMALTU 273
DIMALTTD 274
DIMAUNIT 275
DIMFRAC 276
DIMLUNIT 277
DIMDSEP 278
DIMATMOVE 279
DIMJUST 280
DIMSD1 281
DIMSD2 282
DIMTOLJ 283
DIMTZIN 284
DIMALTZ 285
DIMALTTZ 286
DIMUPT 288
DIMATFIT 289
DIMTXSTY 340
DIMLDRBLK 341
DIMBLK 342
DIMBLK1 343
DIMBLK2 344
DIMLWD 371
DIMLWE 372
Variables have different types: these can be found in
the ObjectARX Reference - search for "Dimension Style
Overrides"
*/
ResultBuffer rb =
new ResultBuffer(
new TypedValue[8]{
new TypedValue(
(int)DxfCode.ExtendedDataRegAppName, "ACAD"
),
new TypedValue(
(int)DxfCode.ExtendedDataAsciiString, "DSTYLE"
),
new TypedValue(
(int)DxfCode.ExtendedDataControlString, "{"
),
new TypedValue(
(int)DxfCode.ExtendedDataInteger16, 77 // DIMTAD
),
new TypedValue(
(int)DxfCode.ExtendedDataInteger16, 4 // Below
),
new TypedValue(
(int)DxfCode.ExtendedDataInteger16, 147 // DIMGAP
),
new TypedValue(
(int)DxfCode.ExtendedDataReal, 0.5 // Larger
),
new TypedValue(
(int)DxfCode.ExtendedDataControlString, "}"
)
}
);
// Set the XData on our object
ad2.XData = rb;
rb.Dispose();
// Now let's open the current space and add our two
// dimensions
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite
);
btr.AppendEntity(ad1);
btr.AppendEntity(ad2);
tr.AddNewlyCreatedDBObject(ad1, true);
tr.AddNewlyCreatedDBObject(ad2, true);
// And commit the transaction, of course
tr.Commit();
}
}
}
}
When we NETLOAD our application and run the ODS command, we see that two dimensions are created in the current space:
I won’t spend time on the specifics of the various dimension variables you can override using this mechanism – I’m sure many (if not most) of you know much more about AutoCAD’s dimension mechanism than I – but I have included the list of the various dimension variables’ group codes in a comment in the above code. For more information regarding the specific types of these variables – and their ranges, if applicable – I suggest searching for “Dimension Style Override” in the ObjectARX Reference.
Update
OK, I missed the obvious on this one (as does happen from time-to-time, as regular readers will by now be aware). Rather than setting the overrides directly via XData, there are handy properties belonging to the dimension’s managed interface that do this for you. Very cool. So we can reduce the code to the following:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace DimStyleOverrideTest
{
public class Commands
{
[CommandMethod("ODS")]
public void OverrideDimensionStyle()
{
Database db =
HostApplicationServices.WorkingDatabase;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
// Open our dimension style table to add our
// new dimension style
DimStyleTable dst =
(DimStyleTable)tr.GetObject(
db.DimStyleTableId, OpenMode.ForWrite
);
// Create our new dimension style
DimStyleTableRecord dstr =
new DimStyleTableRecord();
dstr.Dimtad = 2;
dstr.Dimgap = 0.3;
dstr.Name = "MyStyle";
// Add it to the dimension style table
ObjectId dsId = dst.Add(dstr);
tr.AddNewlyCreatedDBObject(dstr, true);
// Now create two identical dimensions, one
// next to the other, using our dimension
// style
AlignedDimension ad1 =
new AlignedDimension(
Point3d.Origin,
new Point3d(5.0, 0.0, 0.0),
new Point3d(2.5, 2.0, 0.0),
"Standard dimension",
dsId
);
// The only thing we change is the text string
AlignedDimension ad2 =
new AlignedDimension(
new Point3d(5.0, 0.0, 0.0),
new Point3d(10.0, 0.0, 0.0),
new Point3d(7.5, 2.0, 0.0),
"Overridden dimension",
dsId
);
// Isn't this easier?
ad2.Dimtad = 4;
ad2.Dimgap = 0.5;
// Now let's open the current space and add our two
// dimensions
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite
);
btr.AppendEntity(ad1);
btr.AppendEntity(ad2);
tr.AddNewlyCreatedDBObject(ad1, true);
tr.AddNewlyCreatedDBObject(ad2, true);
// And commit the transaction, of course
tr.Commit();
}
}
}
}
I managed to forget to name the style in the previous example, so I’ve gone ahead and fixed that in both sets of code.
And it turns out that the original question may actually have been about something different, so I’m now going to go away and look at that (and will create a new post, as needed).