public class TestClass
{
private ObjectId _parentBlockID;
//private string _parentBlockName = "DynamicRectangle";
[CommandMethod("Tester1")]
public void Test_swamp()
{
XbyXEndView XbyX = null;
Point3d point
= new Point3d
(0,
0,
0); Vector3d offset
= new Vector3d
(2.5,
0,
0); int num = 1;
try
{
while (num <= 1000)
{
//AcadEditor.WriteLine("Generating block reference # " + num.ToString());
XbyX
= new XbyXEndView
(); XbyX.Height = 5.5;
XbyX.Width = 1.5;
Transaction acTransaction = GetAutocadTransaction();
using (acTransaction)
{
BlockTableRecord modelSpace = default(BlockTableRecord);
BlockTable bt = (BlockTable)acTransaction.GetObject(Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
modelSpace = (BlockTableRecord)acTransaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
XbyX.CreateReference(point, modelSpace);
XbyX.SetProperties();
acTransaction.Commit();
}
//acTransaction.Dispose();
num += 1;
point += offset;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
//AcadEditor.ReportError(ex);
}
}
public static Transaction GetAutocadTransaction()
{
//System.Threading.Thread.Sleep(0);
Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction();
//tr.TransactionManager.QueueForGraphicsFlush();
//System.Threading.Thread.Sleep(0);
return tr;
}
// There had to be a better way of getting the objectId from the name!
}
public class XbyXEndView
{
private static ObjectId _parentBlockID = ObjectId.Null;
private string _parentBlockName;
private BlockReference _acBlockReference = null;
private string _layer = "<use current>";
private double _rotation = 0.0;
private double _scaleX = 1.0;
private double _scaleY = 1.0;
private double _scaleZ = 1.0;
public XbyXEndView()
{
_parentBlockName = "XbyX";// End View";
if (_parentBlockID == ObjectId.Null)
{
_parentBlockID = GetParentBlockIdFromName(_parentBlockName);
}
}
public double Width { get; set; }
public double Height { get; set; }
public void SetProperties()
{
//AcadEditor.WriteLine("Attempting to set the properties of block: " + _acBlockReference.Name);
SetProperty("Height", Height);
SetProperty("Width", Width);
}
// There has to be a better way of doing this...
protected ObjectId GetParentBlockIdFromName(string blockName)
{
ObjectId blockId = ObjectId.Null;
Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
using (var tr = e.Document.Database.TransactionManager.StartTransaction())
{
var blockTable = tr.GetObject(e.Document.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
var blockTableRecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (var entityId in blockTableRecord)
{
var dynamicBlock = tr.GetObject(entityId, OpenMode.ForWrite, false) as BlockReference;
if (dynamicBlock != null && !dynamicBlock.IsErased && string.Compare(dynamicBlock.Name, blockName, true) == 0 && dynamicBlock.IsDynamicBlock)
{
blockId
= new ObjectId
(new IntPtr
(dynamicBlock
.DynamicBlockTableRecord.OldId)); break;
}
}
tr.Commit();
}
//Transaction acTransaction = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction();
//using (acTransaction)
//{
// BlockTable blockTable = (BlockTable)acTransaction.GetObject(Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
// SymbolTableEnumerator bTIterator = blockTable.GetEnumerator;
// if (blockTable.Has(blockName))
// {
// bool moreRecords = false;
// BlockTableRecord btr = default(BlockTableRecord);
// ObjectId btrID = default(ObjectId);
// bTIterator.Reset();
// moreRecords = bTIterator.MoveNext;
// bool success = false;
// while (moreRecords)
// {
// btrID = bTIterator.Current;
// btr = (BlockTableRecord)acTransaction.GetObject(btrID, OpenMode.ForRead);
// if (btr.Name == blockName)
// {
// blockId = btr.ObjectId;
// success = true;
// break; // TODO: might not be correct. Was : Exit Do
// }
// moreRecords = bTIterator.MoveNext;
// }
// if (!success)
// {
// throw new System.Exception("Look up failure for block: " + blockName);
// }
// }
// else
// {
// throw new System.Exception("Block" + blockName + " does not exist in the current document...");
// }
// acTransaction.Commit();
//}
return blockId;
}
public void CreateReference(Point3d Location, BlockTableRecord blockTableRec)
{
if (_acBlockReference == null)
{
Transaction transaction = TestClass.GetAutocadTransaction();
using (transaction)
{
_acBlockReference
= new BlockReference
(Location, _parentBlockID
); if (_acBlockReference != null && _acBlockReference.IsDynamicBlock)
{
ObjectId id = blockTableRec.AppendEntity(_acBlockReference);
if (_acBlockReference.IsNewObject)
{
transaction.AddNewlyCreatedDBObject(_acBlockReference, true);
}
//transaction.Commit();
_acBlockReference
.ScaleFactors = new Scale3d
(_scaleX, _scaleY, _scaleZ
); //if (_layer != "<use current>")
//{
// _acBlockReference.Layer = _layer;
//}
_acBlockReference.Rotation = _rotation;
}
transaction.Commit();
}
//System.Threading.Thread.Sleep(50);
//transaction.Dispose();
}
else
{
throw new System.Exception("Reference already created..."); }
}
protected void SetProperty(string Name, double Value)
{
if (_acBlockReference != null) {
//System.Threading.Thread.Sleep(0);
bool success = false;
DynamicBlockReferencePropertyCollection propColl = _acBlockReference.DynamicBlockReferencePropertyCollection;
//DynamicBlockReferenceProperty prop = default(DynamicBlockReferenceProperty);
foreach (DynamicBlockReferenceProperty prop in propColl)
{
if (!prop.ReadOnly && prop.PropertyName.ToUpper() == Name.ToUpper()) {
prop.Value = Value;
//AcadEditor.WriteLine("Successfully set property " + Name + " on block reference: " + _acBlockReference.Name);
success = true;
break; // TODO: might not be correct. Was : Exit For
}
}
if (!success) {
//AcadEditor.WriteLine("Failed to set property " + Name + " on block reference: " + _acBlockReference.Name);
throw new System.Exception(_parentBlockName
+ " does not have a property " + Name
); }
} else {
throw new System.Exception(_parentBlockName
+ " is not a dynamic block."); }
}
}
public class AcadEditor
{
public static void ExecuteCommand(string command)
{
//Application.DocumentManager.MdiActiveDocument.SendStringToExecute(Strings.Chr(27) + Strings.Chr(27) + command, true, false, true);
}
public static void WriteLine(string message)
{
//Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Constants.vbCrLf + message + Constants.vbCrLf);
}
public static void ReportError(System.Exception ex)
{
//string message = ex.Message + Constants.vbCrLf + Constants.vbCrLf + ex.StackTrace;
//message += Constants.vbCrLf + Constants.vbCrLf + ex.Source;
//if (ex.InnerException != null)
//{
// message += Constants.vbCrLf + Constants.vbCrLf + ex.InnerException.Message;
//}
//Interaction.MsgBox(message, MsgBoxStyle.Critical, "Error");
}
}