I am creating a plugin for Autocad 2016 which draws in the program depending on the values that are loaded, I would like it to be possible to automatically dimension the entire drawing in two different dimension styles. I managed to create the dimensions of the same style but I can't switch between two styles.
Example: With this code I dimension the total length of the drawn part, but the part has some circles drawn inside called "Punches" that must also be dimensioned but with another dimension style. opening an autocad template where the dimension styles are already preloaded, all I have to do is switch between one or the other.
public static void AcoteSimple(double Ancho, double Largo_total)
{
var acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var acCurDb = acDoc.Database;
using (acDoc.LockDocument())
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
if (Ancho == 0)
{
Ancho = 300;
}
using (var dim = new AlignedDimension(
new Point3d(0, Ancho, 0), new Point3d(Largo_total, Ancho, 0), new Point3d(0, 300, 0), "LARGO TOTAL: <>", acCurDb.Dimstyle))
{
acBlkTblRec.AppendEntity(dim);
acTrans.AddNewlyCreatedDBObject(dim, true);
}
acTrans.Commit();
}
}
I thought it was going to be something similar when exchanging between layers as seen below but the adaptation I did did not work. (the code below is what I currently use to switch between layers)
public static void Cambiar_Layer(string Layer_Select)
{
// Get the current document and database
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
acDoc.LockDocument();
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
string sLayerName = "";
if (Layer_Select == "L1")
{
sLayerName = "L1";
}
else if (Layer_Select == "L2")
{
sLayerName = "L2";
}
else if (Layer_Select == "L3")
{
sLayerName = "L3";
}
if (acLyrTbl.Has(sLayerName) == true)
{
// Set the layer Center current
acCurDb.Clayer = acLyrTbl[sLayerName];
// Save the changes
acTrans.Commit();
}
// Dispose of the transaction
}
}
Searching the internet I found this code that according to what they say in the forum works for what I need
public void ChangeDimStlye()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trx = db.TransactionManager.StartTransaction())
{
DimStyleTable dimTbl = (DimStyleTable)trx.GetObject(db.DimStyleTableId, OpenMode.ForRead);
DimStyleTableRecord dimDtr = (DimStyleTableRecord)trx.GetObject(dimTbl["DimStyle1"], OpenMode.ForRead);
ObjectIdCollection ids = dimDtr.GetPersistentReactorIds();
foreach (ObjectId id in ids)
{
if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Dimension))))
{
Dimension dim = (Dimension)trx.GetObject(id, OpenMode.ForWrite);
dim.DimensionStyle = dimTbl["DimStyle2"];
//dim.DimensionStyleName = "DimStyle2";
}
}
trx.Commit();
}
}
But it does absolutely nothing. In my autocad template I have 2 Dimensions or Dimensions that are called "DIM20" and "ACUM20" and they are between the two that I need to change to draw the dimensions, but I did not have any success.
How can I achieve my purpose?
1 Answers