[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){try{using(TransactionacTrans=ActiveUtil.TransactionManager.StartTransaction()){// Create a circle that is at 2,3 with a radius of 4.25CircleacCirc=newCircle();acCirc.SetDatabaseDefaults();acCirc.Center=newPoint3d(2,3,0);acCirc.Radius=4.25;// Add the new object to the block table record and the transactionActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(acCirc);acTrans.AddNewlyCreatedDBObject(acCirc,true);// Create a copy of the circle and change its radiusCircleacCircClone=acCirc.Clone()asCircle;acCircClone.Radius=1;// Add the cloned circleActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(acCircClone);acTrans.AddNewlyCreatedDBObject(acCircClone,true);// Save the new object to the databaseacTrans.Commit();}}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}
Copy Item between database
[CommandMethod("Test", CommandFlags.Session)]publicstaticvoidCopyObjectsBetweenDatabases(){ObjectIdCollectionacObjIdColl=newObjectIdCollection();// Lock the current documentusing(DocumentLockacLckDocCur=ActiveUtil.Document.LockDocument()){using(TransactionacTrans=ActiveUtil.TransactionManager.StartTransaction()){// Create a circle that is at (0,0,0) with a radius of 5CircleacCirc1=newCircle();acCirc1.SetDatabaseDefaults();acCirc1.Center=newPoint3d(0,0,0);acCirc1.Radius=5;ActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(acCirc1);acTrans.AddNewlyCreatedDBObject(acCirc1,true);// Create a circle that is at (0,0,0) with a radius of 7CircleacCirc2=newCircle();acCirc2.SetDatabaseDefaults();acCirc2.Center=newPoint3d(0,0,0);acCirc2.Radius=7;ActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(acCirc2);acTrans.AddNewlyCreatedDBObject(acCirc2,true);// Add all the objects to copy to the new documentacObjIdColl=newObjectIdCollection{acCirc1.ObjectId,acCirc2.ObjectId};// Save the new objects to the databaseacTrans.Commit();}// Unlock the document}// Change the file and path to match a drawing template on your workstationstringsLocalRoot=Application.GetSystemVariable("LOCALROOTPREFIX")asstring;stringsTemplatePath=sLocalRoot+"Template\\acad.dwt";// Create a new drawing to copy the objects toDocumentCollectionacDocMgr=Application.DocumentManager;DocumentacNewDoc=acDocMgr.Add(sTemplatePath);DatabaseacDbNewDoc=acNewDoc.Database;// Lock the new documentusing(DocumentLockacLckDoc=acNewDoc.LockDocument()){// Start a transaction in the new databaseusing(TransactionacTrans=acDbNewDoc.TransactionManager.StartTransaction()){// Clone the objects to the new databaseIdMappingacIdMap=newIdMapping();ActiveUtil.Database.WblockCloneObjects(acObjIdColl,acDbNewDoc.GetModelSpace().ObjectId,acIdMap,DuplicateRecordCloning.Ignore,false);// Save the copied objects to the databaseacTrans.Commit();}// Unlock the document}// Set the new document currentacDocMgr.MdiActiveDocument=acNewDoc;}
Copy Block References from external drawing
[CommandMethod("Test")]publicvoidTest(){try{//Code to copy block from another drawing to active drawingstringexternalFilePath=@"C:\Users\Ryzen2600x\source\repos\Template_AutoCAD_BasicApp_CSharp\CadApp\Sample.dwg";DatabasetempDatabase=newDatabase(false,true);tempDatabase.ReadDwgFile(externalFilePath,System.IO.FileShare.ReadWrite,true,"");ObjectIdCollectionblockObjectIdCollection=newObjectIdCollection();using(Transactiontransaction=tempDatabase.TransactionManager.StartTransaction()){BlockTableblockTable=transaction.GetObject(tempDatabase.BlockTableId,OpenMode.ForRead)asBlockTable;//Check if drawing contain specific blockif(blockTable.Has("Pole")){blockObjectIdCollection.Add(blockTable["Pole"]);}transaction.Commit();}if(blockObjectIdCollection.Count==0){return;}IdMappingidMapping=newIdMapping();ActiveUtil.Database.WblockCloneObjects(blockObjectIdCollection,ActiveUtil.Database.BlockTableId,idMapping,DuplicateRecordCloning.Ignore,false);//Code to loop through all block references and Copy Exiting referencesObjectIdCollectionblockRefObjectIdCollection=newObjectIdCollection();using(Transactiontransaction=tempDatabase.TransactionManager.StartTransaction()){BlockTableblockTable=transaction.GetObject(tempDatabase.BlockTableId,OpenMode.ForRead)asBlockTable;BlockTableRecordmodelSpaceBlockTable=transaction.GetObject(blockTable[BlockTableRecord.ModelSpace],OpenMode.ForRead)asBlockTableRecord;foreach(ObjectIdobjectIdinmodelSpaceBlockTable){if(objectId.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference)))){BlockReferenceblockReference=transaction.GetObject(objectId,OpenMode.ForRead)asBlockReference;if(blockReference.Name!="Pole"){continue;}//Create new Block Reference on Active Database//Copy Block Attribute values from original using(TransactionsubTransaction=ActiveUtil.Document.TransactionManager.StartTransaction()){//Open block table of active drawingBlockTableactiveblockTable=subTransaction.GetObject(ActiveUtil.Database.BlockTableId,OpenMode.ForRead)asBlockTable;//Opem Model Space block table for active drawingBlockTableRecordcurrentActiveSpaceBlockTableRecord=subTransaction.GetObject(ActiveUtil.Database.CurrentSpaceId,OpenMode.ForWrite)asBlockTableRecord;if(!activeblockTable.Has("Pole")){return;}//Get Access to block definationBlockTableRecordactiveBlockTableRecord=subTransaction.GetObject(activeblockTable["Pole"],OpenMode.ForRead)asBlockTableRecord;//Create new Block referenceBlockReferencenewBlockReference=newBlockReference(blockReference.Position,activeBlockTableRecord.ObjectId);//Add block reference to active spacecurrentActiveSpaceBlockTableRecord.AppendEntity(newBlockReference);//Add blockReference to current transactionsubTransaction.AddNewlyCreatedDBObject(newBlockReference,true);//Iterate block definition to find all non-constant AttributeDefinitionsforeach(ObjectIdidinactiveBlockTableRecord){AttributeDefinitionattDef=subTransaction.GetObject(id,OpenMode.ForRead)asAttributeDefinition;if((attDef!=null)&&(!attDef.Constant)){using(AttributeReferenceattRef=newAttributeReference()){attRef.SetAttributeFromBlock(attDef,newBlockReference.BlockTransform);attRef.TextString=attDef.TextString;//Add the AttributeReference to the BlockReferencenewBlockReference.AttributeCollection.AppendAttribute(attRef);subTransaction.AddNewlyCreatedDBObject(attRef,true);}}}subTransaction.Commit();}}}transaction.Commit();}tempDatabase.Dispose();}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}