.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Content missing in the dwg file after using SaveAs() in my script

8 REPLIES 8
Reply
Message 1 of 9
TXACDE24
372 Views, 8 Replies

Content missing in the dwg file after using SaveAs() in my script

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);
}
}

8 REPLIES 8
Message 2 of 9
norman.yuan
in reply to: TXACDE24

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.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 9
Jeff_M
in reply to: TXACDE24

In addition to @norman.yuan response, when posting code please use the code tag so it will keep the formatting.

Code tag.png

Jeff_M, also a frequent Swamper
EESignature
Message 4 of 9
TXACDE24
in reply to: norman.yuan

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.

TXACDE24_0-1720894243047.png

[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);
    }
}

 

 

 

Message 5 of 9
norman.yuan
in reply to: TXACDE24

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?

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 9
TXACDE24
in reply to: norman.yuan

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.

TXACDE24_0-1720902144974.png

However, the Symbol Builder created symbol would show the Attribute Editor when double clicked.

TXACDE24_1-1720902182183.png

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
    }

 

Message 7 of 9
ActivistInvestor
in reply to: TXACDE24

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.

Message 8 of 9
TXACDE24
in reply to: ActivistInvestor

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.

Message 9 of 9
ActivistInvestor
in reply to: TXACDE24


@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.

Post to forums  

Forma Design Contest


AutoCAD Beta