The build #0.7.5.0 of AutoCAD .Net Addin Wizard (AcadNetAddinWizard) has been rolled out and provided a few new features. The Entity Appender coder is one of them.
If the cursor is inside a method body like the following and the Entity Appender is set as the above,
after the OK button is pressed, the following code snippet will be appended automatically to the method.
public static ObjectId CreateCircle()
{
//ObjectId entId = ObjectId.Null;
using (Circle newEnt = new Circle()) //The default parameterless constructor
{
//Initialize necessary properties below
//newEnt.ColorIndex = 1;
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
using (trans)
{
BlockTableRecord blkTR = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
newEnt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
blkTR.AppendEntity(newEnt);
trans.AddNewlyCreatedDBObject(newEnt, true);
//entId = newEnt.ObjectId;
trans.Commit();
}
}
}
Through few adjustments being made to the snippet as instructed by the comments, the method will become functional. Here is a good example.
public static ObjectId CreateCircle()
{
ObjectId entId = ObjectId.Null; //uncommented out
using (Circle newEnt = new Circle()) //The default parameterless constructor
{
//Initialize necessary properties below
newEnt.ColorIndex = 1; //uncommented out
newEnt.Center = new Point3d(0, 0, 0); //added
newEnt.Radius = 10.0; //added
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
using (trans)
{
BlockTableRecord blkTR = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
newEnt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
blkTR.AppendEntity(newEnt);
trans.AddNewlyCreatedDBObject(newEnt, true);
entId = newEnt.ObjectId; //uncommented out
trans.Commit();
}
}
return entId; //added
}
Here are some more different flavors of the code generated by the Entity Appender coder.
public static void CreateCircle1()
{
//ObjectId entId = ObjectId.Null;
using (Circle newEnt = new Circle()) //The default parameterless constructor
{
//Initialize necessary properties below
//newEnt.ColorIndex = 1;
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.TopTransaction;
BlockTableRecord blkTR = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
newEnt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
blkTR.AppendEntity(newEnt);
trans.AddNewlyCreatedDBObject(newEnt, true);
//entId = newEnt.ObjectId;
//trans.Commit();
}
}
public static void CreateCircle2()
{
//ObjectId entId = ObjectId.Null;
using (Circle newEnt = new Circle()) //The default parameterless constructor
{
//Initialize necessary properties below
//newEnt.ColorIndex = 1;
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartOpenCloseTransaction();
using (trans)
{
BlockTableRecord blkTR = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
newEnt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
blkTR.AppendEntity(newEnt);
trans.AddNewlyCreatedDBObject(newEnt, true);
//entId = newEnt.ObjectId;
trans.Commit();
}
}
}
public static void CreateCircle3(Transaction trans)
{
//ObjectId entId = ObjectId.Null;
using (Circle newEnt = new Circle()) //The default parameterless constructor
{
//Initialize necessary properties below
//newEnt.ColorIndex = 1;
BlockTableRecord blkTR = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
newEnt.TransformBy(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
blkTR.AppendEntity(newEnt);
trans.AddNewlyCreatedDBObject(newEnt, true);
//entId = newEnt.ObjectId;
//trans.Commit();
}
}
The Database instance can also be passed from outside as method argument or created/obtained a bit earlier in the method. Please give it a try and feel its power and flexibility yourself.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders to help program AutoCAD .NET addins.
Recent Comments