Community
//Hello my name is Dario //'m from Brazil //I'm still having problem //need help //can someone help me? //insert a block, and in the sequel want the data of this block, //and then delete this block //as the example in the main code, just below .. //I have these questions that are commented no Main method >>??????? //Main method //=================================================================================================== [CommandMethod("Blockk")] public void blockk() { Document doc = Host.ApplicationServices.Application.DocumentManager.MdiActiveDocument; string[] data = new string[3]; data[0] = "data 1"; data[1] = "data 2"; data[2] = "data 3"; BlockReference db = InsertBlockFromFile_(doc, @"C:\wbc\orienta.dwg", new Point3d(50, 50, 50), Math.PI * 0.25, "wbc_insert", data, 30, 30, 1, 7, true, null, "wbcd"); //??????? return..>>> Example //??????? db.insertionPoint //??????? db.angle //??????? db.handle //??????? db.Xproprierts //??????? db.Atrib //??????? db.erase } //Complementary methods //=============================================================================================================================== public static BlockReference InsertBlockFromFile_(Document doc, string DrawingFile, Point3d Pinsert, double Angle, string Layer, string[] DadosExtendidos = null, double escalax = 1.0, double escalay = 1.0, double escalaz = 1.0, short ColorLayer = 7, bool ModelSpace = true, string[] Atributos = null, string RegAplicacao = "wbc") { if (!System.IO.File.Exists(DrawingFile)) { MessageBox.Show("Drawing not found\n" + DrawingFile); return null; } string sourceFileName = DrawingFile; string Blockname = System.IO.Path.GetFileNameWithoutExtension(sourceFileName); try { using (Transaction tr = doc.TransactionManager.StartTransaction()) { Database db = new Database(false, false); db.ReadDwgFile(DrawingFile, System.IO.FileShare.Read, true, null); ObjectId BlkId = doc.Database.Insert(Blockname, db, true); // ISL: Small fix, use correct block name tr.Commit(); } } catch (IOException ioe) { MessageBox.Show(ioe.Message); return null; } return AddBlockTest_(doc, Blockname, Pinsert, Angle, Layer, DadosExtendidos, escalax, escalay, escalaz, ColorLayer, Atributos, RegAplicacao); } private static BlockReference AddBlockTest_(Document doc, string blockName, Point3d Pinsert, double Angle, string Layer, string[] DadosExtendidos = null, double escalax = 1.0, double escalay = 1.0, double escalaz = 1.0, short ColorLayer = 7, string[] Atributos = null, string Regaplicacao = "wbc") { Database db = doc.Database; using (Transaction myT = db.TransactionManager.StartTransaction()) { //Get the block definition "Check". BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord blockDef = bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord; BlockTableRecord ms = (BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); //Create new BlockReference, and link it to our block definition using (BlockReference blockRef = new BlockReference(Pinsert, blockDef.ObjectId)) { blockRef.Rotation = Angle; blockRef.ScaleFactors = new Scale3d(escalax, escalay, escalaz); //Add the block reference to modelspace ms.AppendEntity(blockRef); myT.AddNewlyCreatedDBObject(blockRef, true); SetXData_(blockRef, Regaplicacao , DadosExtendidos, doc); CreatLayer_(Layer, doc, ColorLayer); blockRef.Layer = Layer; } //Our work here is done myT.Commit(); } return null; } static public void SetXData_(Entity bref, string nome, string[] data, Document doc, string VersaoEquipamento = "Without Version") { Database db = doc.Database; using (Transaction tr = doc.TransactionManager.StartTransaction()) { AddRegAppTableRecord_(nome, doc); ResultBuffer rb = new ResultBuffer(); rb.Add(new TypedValue(1001, nome)); for (int i = 0; i < data.Length; i++) { rb.Add(new TypedValue(1000, data[i])); } rb.Add(new TypedValue(1000, VersaoEquipamento)); DateTime Wdata = DateTime.Now; Wdata.ToShortDateString(); rb.Add(new TypedValue(1000, Wdata.ToString())); rb.Add(new TypedValue(1000, VariaveisGlobais.Wusuario + "_" + VariaveisGlobais.NumeroSerie)); bref.XData = rb; tr.Commit(); } } public static void CreatLayer_(string sLayerName, Document doc, short CorCamada = 7) { Document acDoc = doc; Database acCurDb = acDoc.Database; // Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Layer table for read LayerTable acLyrTbl; acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; if (acLyrTbl.Has(sLayerName) == false) { Platform.DatabaseServices.LayerTableRecord acLyrTblRec = new LayerTableRecord(); // Assign the layer the ACI color 1 and a name acLyrTblRec.Color = Platform.Colors.Color.FromColorIndex(Platform.Colors.ColorMethod.ByAci, CorCamada); acLyrTblRec.Name = sLayerName; // Upgrade the Layer table for write acLyrTbl.UpgradeOpen(); // Append the new layer to the Layer table and the transaction acLyrTbl.Add(acLyrTblRec); acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true); } acTrans.Commit(); } } public static void AddRegAppTableRecord_(string regAppName, Document doc) { using (Transaction tr = doc.TransactionManager.StartTransaction()) { RegAppTable rat = (RegAppTable)tr.GetObject(doc.Database.RegAppTableId, OpenMode.ForRead, false); if (!rat.Has(regAppName)) { rat.UpgradeOpen(); RegAppTableRecord ratr = new RegAppTableRecord(); ratr.Name = regAppName; rat.Add(ratr); tr.AddNewlyCreatedDBObject(ratr, true); } tr.Commit(); } }
Hi Dario,
Sorry for the long delay.
Is this topic still a problem ?
The issue with your code seems more related to the structure of your code. Make sure each method does a specific task, like creating a block definition, inserting a block reference, modifying the block reference etc. Now ensure that those methods accept or return the ObjectId of the entity on which they will work. This should help in getting it to work in sequence.
Hope this helps. If not, please do let me know.
Regards,
Balaji