Author Topic: Transaction timing issues...  (Read 18715 times)

0 Members and 1 Guest are viewing this topic.

LE3

  • Guest
Re: Transaction timing issues...
« Reply #45 on: March 16, 2013, 07:12:59 PM »
was able to play a little more, using your code but converted to c# in one of the online tools on the net, did some minor changes, and all tests runs with no error.

Code - C#: [Select]
  1.     public class TestClass
  2.     {
  3.  
  4.         private ObjectId _parentBlockID;
  5.  
  6.         //private string _parentBlockName = "DynamicRectangle";
  7.         [CommandMethod("Tester1")]
  8.         public void Test_swamp()
  9.         {
  10.             XbyXEndView XbyX = null;
  11.             Point3d point = new Point3d(0, 0, 0);
  12.             Vector3d offset = new Vector3d(2.5, 0, 0);
  13.             int num = 1;
  14.             try
  15.             {
  16.                 while (num <= 1000)
  17.                 {
  18.                     //AcadEditor.WriteLine("Generating block reference # " + num.ToString());
  19.                     XbyX = new XbyXEndView();
  20.                     XbyX.Height = 5.5;
  21.                     XbyX.Width = 1.5;
  22.                     Transaction acTransaction = GetAutocadTransaction();
  23.                     using (acTransaction)
  24.                     {
  25.                         BlockTableRecord modelSpace = default(BlockTableRecord);
  26.                         BlockTable bt = (BlockTable)acTransaction.GetObject(Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
  27.                         modelSpace = (BlockTableRecord)acTransaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  28.                         XbyX.CreateReference(point, modelSpace);
  29.                         XbyX.SetProperties();
  30.                         acTransaction.Commit();
  31.                     }
  32.                     //acTransaction.Dispose();
  33.                     num += 1;
  34.                     point += offset;
  35.                 }
  36.             }
  37.             catch (Autodesk.AutoCAD.Runtime.Exception ex)
  38.             {
  39.                 //AcadEditor.ReportError(ex);
  40.             }
  41.  
  42.         }
  43.  
  44.         public static Transaction GetAutocadTransaction()
  45.         {
  46.             //System.Threading.Thread.Sleep(0);
  47.             Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction();
  48.             //tr.TransactionManager.QueueForGraphicsFlush();
  49.             //System.Threading.Thread.Sleep(0);
  50.             return tr;
  51.         }
  52.  
  53.         // There had to be a better way of getting the objectId from the name!
  54.  
  55.     }
  56.  
  57.     public class XbyXEndView
  58.     {
  59.  
  60.         private static ObjectId _parentBlockID = ObjectId.Null;
  61.         private string _parentBlockName;
  62.         private BlockReference _acBlockReference = null;
  63.         private string _layer = "<use current>";
  64.         private double _rotation = 0.0;
  65.         private double _scaleX = 1.0;
  66.         private double _scaleY = 1.0;
  67.  
  68.         private double _scaleZ = 1.0;
  69.         public XbyXEndView()
  70.         {
  71.             _parentBlockName = "XbyX";// End View";
  72.             if (_parentBlockID == ObjectId.Null)
  73.             {
  74.                 _parentBlockID = GetParentBlockIdFromName(_parentBlockName);
  75.             }
  76.         }
  77.  
  78.         public double Width { get; set; }
  79.         public double Height { get; set; }
  80.  
  81.         public void SetProperties()
  82.         {
  83.             //AcadEditor.WriteLine("Attempting to set the properties of block: " + _acBlockReference.Name);
  84.             SetProperty("Height", Height);
  85.             SetProperty("Width", Width);
  86.         }
  87.  
  88.         //   There has to be a better way of doing this...
  89.         protected ObjectId GetParentBlockIdFromName(string blockName)
  90.         {
  91.             ObjectId blockId = ObjectId.Null;
  92.             Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  93.             using (var tr = e.Document.Database.TransactionManager.StartTransaction())
  94.             {
  95.                 var blockTable = tr.GetObject(e.Document.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
  96.                 var blockTableRecord = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  97.                 foreach (var entityId in blockTableRecord)
  98.                 {
  99.                     var dynamicBlock = tr.GetObject(entityId, OpenMode.ForWrite, false) as BlockReference;
  100.                     if (dynamicBlock != null && !dynamicBlock.IsErased && string.Compare(dynamicBlock.Name, blockName, true) == 0 && dynamicBlock.IsDynamicBlock)
  101.                     {
  102.                         blockId = new ObjectId(new IntPtr(dynamicBlock.DynamicBlockTableRecord.OldId));
  103.                         break;
  104.                     }
  105.                 }
  106.                 tr.Commit();
  107.             }
  108.  
  109.             //Transaction acTransaction = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction();
  110.             //using (acTransaction)
  111.             //{
  112.             //    BlockTable blockTable = (BlockTable)acTransaction.GetObject(Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
  113.             //    SymbolTableEnumerator bTIterator = blockTable.GetEnumerator;
  114.             //    if (blockTable.Has(blockName))
  115.             //    {
  116.             //        bool moreRecords = false;
  117.             //        BlockTableRecord btr = default(BlockTableRecord);
  118.             //        ObjectId btrID = default(ObjectId);
  119.             //        bTIterator.Reset();
  120.             //        moreRecords = bTIterator.MoveNext;
  121.             //        bool success = false;
  122.             //        while (moreRecords)
  123.             //        {
  124.             //            btrID = bTIterator.Current;
  125.             //            btr = (BlockTableRecord)acTransaction.GetObject(btrID, OpenMode.ForRead);
  126.             //            if (btr.Name == blockName)
  127.             //            {
  128.             //                blockId = btr.ObjectId;
  129.             //                success = true;
  130.             //                break; // TODO: might not be correct. Was : Exit Do
  131.             //            }
  132.             //            moreRecords = bTIterator.MoveNext;
  133.             //        }
  134.             //        if (!success)
  135.             //        {
  136.             //            throw new System.Exception("Look up failure for block: " + blockName);
  137.             //        }
  138.             //    }
  139.             //    else
  140.             //    {
  141.             //        throw new System.Exception("Block" + blockName + " does not exist in the current document...");
  142.             //    }
  143.             //    acTransaction.Commit();
  144.             //}
  145.             return blockId;
  146.         }
  147.  
  148.         public void CreateReference(Point3d Location, BlockTableRecord blockTableRec)
  149.         {
  150.             if (_acBlockReference == null)
  151.             {
  152.                 Transaction transaction = TestClass.GetAutocadTransaction();
  153.                 using (transaction)
  154.                 {
  155.                     _acBlockReference = new BlockReference(Location, _parentBlockID);
  156.                     if (_acBlockReference != null && _acBlockReference.IsDynamicBlock)
  157.                     {
  158.                         ObjectId id = blockTableRec.AppendEntity(_acBlockReference);
  159.                         if (_acBlockReference.IsNewObject)
  160.                         {
  161.                             transaction.AddNewlyCreatedDBObject(_acBlockReference, true);
  162.                         }
  163.                         //transaction.Commit();
  164.                         _acBlockReference.ScaleFactors = new Scale3d(_scaleX, _scaleY, _scaleZ);
  165.                         //if (_layer != "<use current>")
  166.                         //{
  167.                         //    _acBlockReference.Layer = _layer;
  168.                         //}
  169.                         _acBlockReference.Rotation = _rotation;
  170.                     }
  171.                     transaction.Commit();
  172.                 }
  173.                 //System.Threading.Thread.Sleep(50);
  174.                 //transaction.Dispose();
  175.             }
  176.             else
  177.             {
  178.                 throw new System.Exception("Reference already created...");
  179.             }
  180.         }
  181.  
  182.         protected void SetProperty(string Name, double Value)
  183.                 {
  184.                         if (_acBlockReference != null) {
  185.                                 //System.Threading.Thread.Sleep(0);
  186.                                 bool success = false;
  187.                                 DynamicBlockReferencePropertyCollection propColl = _acBlockReference.DynamicBlockReferencePropertyCollection;
  188.                                 //DynamicBlockReferenceProperty prop = default(DynamicBlockReferenceProperty);
  189.                 foreach (DynamicBlockReferenceProperty prop in propColl)
  190.                 {
  191.                                         if (!prop.ReadOnly && prop.PropertyName.ToUpper() == Name.ToUpper()) {
  192.                                                 prop.Value = Value;
  193.                                                 //AcadEditor.WriteLine("Successfully set property " + Name + " on block reference: " + _acBlockReference.Name);
  194.                                                 success = true;
  195.                                                 break; // TODO: might not be correct. Was : Exit For
  196.                                         }
  197.                                 }
  198.                                 if (!success) {
  199.                                         //AcadEditor.WriteLine("Failed to set property " + Name + " on block reference: " + _acBlockReference.Name);
  200.                                         throw new System.Exception(_parentBlockName + " does not have a property " + Name);
  201.                                 }
  202.                         } else {
  203.                                 throw new System.Exception(_parentBlockName + " is not a dynamic block.");
  204.                         }
  205.                 }
  206.     }
  207.  
  208.     public class AcadEditor
  209.     {
  210.  
  211.         public static void ExecuteCommand(string command)
  212.         {
  213.             //Application.DocumentManager.MdiActiveDocument.SendStringToExecute(Strings.Chr(27) + Strings.Chr(27) + command, true, false, true);
  214.         }
  215.  
  216.         public static void WriteLine(string message)
  217.         {
  218.             //Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Constants.vbCrLf + message + Constants.vbCrLf);
  219.         }
  220.  
  221.         public static void ReportError(System.Exception ex)
  222.         {
  223.             //string message = ex.Message + Constants.vbCrLf + Constants.vbCrLf + ex.StackTrace;
  224.             //message += Constants.vbCrLf + Constants.vbCrLf + ex.Source;
  225.             //if (ex.InnerException != null)
  226.             //{
  227.             //    message += Constants.vbCrLf + Constants.vbCrLf + ex.InnerException.Message;
  228.             //}
  229.             //Interaction.MsgBox(message, MsgBoxStyle.Critical, "Error");
  230.         }
  231.  
  232.     }
  233.