Hi,
Any ideas why the circle is missing in the program below?
It is pretty straight forward with creating a block and saving the db to a dwg file, but I cannot find any trace of the circle when opening the Test.dwg file.
[CommandMethod("CreatingBlock")]
public void CreatingBlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
//assign file name and determine if its is named
string strDWGName = Directory.GetCurrentDirectory() + $"\\Results\\Test.dwg";
try
{
// Save the active drawing
acCurDb.SaveAs(strDWGName, true, DwgVersion.Current, acCurDb.SecurityParameters);
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
}
I assume the current MDiActiveDocument is a blank drawing. So, your code does:
1. Create a BlockTableRecord (block definition), called "CircleBlock";
2. Add a circle to the BlockTableRecord;
3. Add the BlockTableRecord into the BlockTable.
That is, the code create a block definition with a Circle as its element entity.
4. Then, you save the current drawing as "Test.dwg"
Now, you say you cannot "find any trace of the circle". How do you do the "FIND"? Are you looking at the ModelSpace tab of the Editor? If, so, of course you cannot see it there: your code creates a BLOCK DEFINITION, it is in block table. If you run "INSERT" command, you would see a block definition "CircleBlock" is there and you can select and insert a BlockReference of it into Model/PaperSpace.
In addition to @norman.yuan response, when posting code please use the code tag so it will keep the formatting.
I opened the saved Test.dwg file and there is no circle around (0,0,0). See my cursor is at about (2, 0) and there is nothing there. I also attached the formatted code.
[CommandMethod("CreatingBlock")]
public void CreatingBlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
//assign file name and determine if its is named
string strDWGName = Directory.GetCurrentDirectory() + $"\\Results\\Test.dwg";
try
{
// Save the active drawing
acCurDb.SaveAs(strDWGName, true, DwgVersion.Current, acCurDb.SecurityParameters);
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
}
Again, what you see in AutoCAD editor, after you open the "Test.dwg", is Model layout, where you DID add the circle to. You add a circle to the BLOCK DEFINITION, called "CircleBlock", which is not visible. If you run command BEdit, and select the block "CircleBlock", you would see there is a circle IN THE BLOCK DEFINITION.
Only BlockReference is see-able, if you insert a BlockReference, based in the block definition "CircleBlock". Not sure what exactly your code is intended to do: creating a block definition only, or you want to create a block definition and then insert a block reference of it?
Hi Norman,
My code tries to create a simple AutoCAD symbol (one circle and one attribute) in lieu of using the Symbol Builder.
I have updated the code with your recommendation (adding BlockReference), but the symbol it creates still cannot be used (or it is different from what Symbol Builder creates).
The program created symbol would show Eid Block Reference when double clicked on a diagram.
However, the Symbol Builder created symbol would show the Attribute Editor when double clicked.
Any idea why?
The modified code is below.
[CommandMethod("CreatingBlock")]
public void CreatingBlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
// Add an attribute definition to the block
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = new Point3d(0, 0, 0);
acAttDef.Prompt = "Custom Attribute";
acAttDef.Tag = "MyAttribute";
acAttDef.TextString = "MyAttributeDefValue";
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.BottomLeft;
acBlkTblRec.AppendEntity(acAttDef);
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), acBlkTblRec.Id))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
using (AttributeReference acAttRef = new AttributeReference())
{
//set the position of the attribute reference
acAttRef.SetAttributeFromBlock(acAttDef, Matrix3d.Displacement(new Vector3d(0, 0, 0)));
acAttRef.Tag = "MyAttribute";
acAttRef.TextString = "MyAttributeRefValue";
acAttRef.Invisible = false;
acAttRef.Height = 1;
acAttRef.Justify = AttachmentPoint.BottomLeft;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
You're working in AutoCAD electrical with a feature you refer to as 'Symbol Builder'. How do you know that 'Symbol Builder' isn't attaching some additional information to the block definition that gives it special significance or meaning to that feature?
Have you looked at an equivalent block reference and block definition created using the Symbol Builder to see if it is attaching some XData/XRecord data to the BlockTableRecord or something contained in it?
I'm asking this, because I don't see any point to a feature like that, if all it does is define generic AutoCAD blocks.
Are you aware of any XData/XRecord data in Symbol Builder?
I skipped all AutoCAD Electrical attributes and strictly built a block in "Symbol Builder" with one circle and one attribute reference to mimic my code.
@TXACDE24 wrote:Are you aware of any XData/XRecord data in Symbol Builder?
I skipped all AutoCAD Electrical attributes and strictly built a block in "Symbol Builder" with one circle and one attribute reference to mimic my code.
You can use MgdDbg to look at xdata/xrecords attached to any object in a .DWG file.
Can't find what you're looking for? Ask the community or share your knowledge.