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

Blockreferences get disappeared after regen command

2 REPLIES 2
Reply
Message 1 of 3
1930331246064
146 Views, 2 Replies

Blockreferences get disappeared after regen command

public static void InsertBlock(Database db, Editor ed, Point2d center2d, double rotationAngle, string blockName, string layerName)
{
   try
   {
      string blockNumberString = mmsBlockNumber.ToString();
      if (blockNumberString.Length == 1)
      {
         blockNumberString = "0" + blockNumberString;
      }
      // Start a transaction
      using (Transaction trans = db.TransactionManager.StartTransaction())
      {
         // Open the Block table for read
         BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);

         // Check if the block definition exists
         if (!bt.Has(blockName))
         {
            ed.WriteMessage("\nBlock \"" + blockName + "\" not found.");
            return;
         }

         // Get the block definition
         ObjectId btrId = bt[blockName];
         BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);

         // Open the current space (model space or paper space) for write
         BlockTableRecord currentSpace = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

         // Create a block reference
         BlockReference blockRef = new BlockReference(new Point3d(center2d.X, center2d.Y, 0), btrId);
         if (!layerName.Contains("Earth")) blockRef.ScaleFactors = new Scale3d(2, 2, 1);
         blockRef.Layer = layerName + " BL-" + blockNumberString;
         // Add the block reference to the current space
         currentSpace.AppendEntity(blockRef);
         trans.AddNewlyCreatedDBObject(blockRef, true);

         // Rotate the block reference
         string basePoint = center2d.X.ToString() + "," + center2d.Y.ToString();
         ed.Command("rotate", blockRef.ObjectId, "", basePoint, rotationAngle);

         // Commit the transaction
         trans.Commit();
      }
   }
   catch (Autodesk.AutoCAD.Runtime.Exception ex)
   {
      // Log specific AutoCAD exceptions for the entire function
      Log.Logger.LogException(ex);
   }
   catch (System.Exception ex)
   {
      // Log any other general exceptions for the entire function
      Log.Logger.LogException(ex);
   }
}

In above code (.NET api), i am trying to add blockreferences in my drawing and it is working perfectly, but if I add blockreferece once, and after that if i add blockreference again and call regen command, the blockreference added first time got disappeared, what should I do? 

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: 1930331246064

You SHOULD NOT call Editor.Command() for rotating the new BlockReference within the Transaction, while it has not been committed.

 

You can simply set the Rotation property of the block (if there is not AttributReference in the BlockReference and the roration is based on the insertion point.), or call TransformBy() to rotate it:

 

var mt=Matrix3d.Rotation(rotationAngle, Vector.ZAxis, basePoint);

blockRef.TransformBy(mt);

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
1930331246064
in reply to: norman.yuan

got the error, actually, in other function, i am erasing the blocks by mistake

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta