Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I have some codes to test block objects as below code.
With:
Command "CircleCall" to create circle blocks (from class "BlockCircle")
Command "CircleTest" to test block object which created by "CircleCall" is from class "BlockCircle"
My problem is command "CircleTest" not work as wishes.
Can anyone help me!
Many thanks for your help.
[CommandMethod("CircleCall")]
public void cmd_CircleCall()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var ppo = new PromptPointOptions("\nPlease select point");
var poi = ed.GetPoint(ppo).Value;
var blockCircle = new BlockCircle();
blockCircle.BlCircle(poi, 4);
}
[CommandMethod("CircleTest")]
public void cmd_CircleTest()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
using (Transaction trx = db.TransactionManager.StartTransaction())
{
PromptEntityOptions peo = new PromptEntityOptions("\nPlease select block object");
peo.SetRejectMessage("Not a Block");
peo.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
BlockReference bref = trx.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference;
BlockTableRecord btr = (BlockTableRecord)trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
if (bref is BlockCircle)
ed.WriteMessage("\nBlock object is Blockcircle");
else
ed.WriteMessage("\nBlock object is not Blockcircle");
trx.Commit();
}
}
public class BlockCircle
{
public string Name;
public Point3d Basepoint;
public void BlCircle(Point3d p,double radius)
{
var db = HostApplicationServices.WorkingDatabase;
this.Basepoint = p;
using (var tr = db.TransactionManager.StartTransaction())
{
BlockTable bltb = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var modelspace = (BlockTableRecord)tr.GetObject(bltb[BlockTableRecord.ModelSpace], OpenMode.ForRead);
var maxIndex = 0;
foreach(ObjectId id in modelspace)
{
if (id.ObjectClass.DxfName == "INSERT")
{
var blockref = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
if (blockref.Name.StartsWith ("Circleblock."))
{
int num = blockref.Name.IndexOf(".");
var curIndex = int.Parse(blockref.Name.Substring(num+1));
if (curIndex > maxIndex)
maxIndex = curIndex;
}
}
}
var bltbrc = new BlockTableRecord();
bltbrc.Name = "Circleblock."+(maxIndex+1).ToString();
this.Name = bltbrc.Name;
if (!bltb.Has(bltbrc.Name))
{
bltbrc.Origin = new Point3d(0, 0, 0);
using (Circle dt = new Circle())
{
dt.Center = new Point3d(0, 0, 0);
dt.Radius = radius;
bltbrc.AppendEntity(dt);
tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
bltb.Add(bltbrc);
tr.AddNewlyCreatedDBObject(bltbrc, true);
}
}
MyPlugin k = new MyPlugin();
k.InsertBlockwithRotation(p,bltbrc.Name,0);
tr.Commit();
}
}
}
public void InsertBlockwithRotation(Point3d insPt, string blockName, double beta){...}
Solved! Go to Solution.