Message 1 of 3
Not applicable
08-18-2019
05:04 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have an INSERT command in my code for a dynamic block because I want the user to place the block. I then use a selection filter to find the recently inserted block based on an attribute with a set default string. Once I find it I change the blocks attribute string to something else that I need.
Is there a way of getting the block before it is put into the database so I don't have to look for it to change its attributes?
I was a database event may do the trick?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using app = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
namespace placePillars
{
public class Class1
{
[CommandMethod("AddPlObjEvent")]
public void AddPlObjEvent()
{
InfBar.PointMonitorTooltips.StartMonitor();
}
[CommandMethod("IBA")]
async public void InsertBlockAsync()
{
var doc =
app.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
//PromptSelectionOptions
//ed.GetSelection();
//Store the handle of the parent
var parentHandle = "";
// Let's ask the user to select the insertion point
await ed.CommandAsync("_.INSERT", "N-PILLAR", Editor.PauseToken, 1, 1, 0);
ed.WriteMessage("\nWe have inserted our block.");
// Get the current document and database, and start a transaction
Document acDoc = app.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//Look through the Block table record
foreach (ObjectId recordID in acBlkTblRec)
{
//If the block is an inserted block then true
if (recordID.ObjectClass.DxfName == "INSERT")
{
BlockReference blkref = (BlockReference)acTrans.GetObject(recordID, OpenMode.ForRead);
AttributeCollection attributes = blkref.AttributeCollection;
//var ObjHandle = blkref.Handle.Value;
//int PilNo = 1;
//Store data for later modules
//Data.DataTable.storeData(ObjHandle, PilNo);
//Look for if the block has the attribute Parent
foreach (ObjectId attId in attributes)
{
AttributeReference attref = (AttributeReference)acTrans.GetObject(attId, OpenMode.ForRead);
if (attref.Tag == "Parent")
{
if (attref.TextString == "Unassigned")
{
//attref.TextString = Parent;
}
}
//MessageBox.Show(recordID.ObjectClass.DxfName);
//break;
}
}
}
acTrans.Commit();
}
}
}
}
Solved! Go to Solution.