代码之家  ›  专栏  ›  技术社区  ›  programmerJavaPL

在C#中使用动态块(对于AutoCad)

  •  0
  • programmerJavaPL  · 技术社区  · 8 年前

    我为矩形创建了一个带有两个参数(par\u l和par\u h)的动态块。为什么在这种情况下什么都没发生?我假设par_l应该将项目扩展到500。默认情况下,我有100和100。

    [CommandMethod("Elem")]
            public void TestCommand()
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
    
                Transaction tr = db.TransactionManager.StartTransaction();
    
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        ed.WriteMessage(bt["prz_podl"]+"");
    
                        BlockTableRecord btr = tr.GetObject(bt["prz_podl"], OpenMode.ForWrite) as BlockTableRecord;
    
                        Point3d point = new Point3d(0, 0, 0);
                        BlockReference br = new BlockReference(point, btr.Id);
                        br.BlockTableRecord = btr.Id;
    
                        DynamicBlockReferencePropertyCollection properties = br.DynamicBlockReferencePropertyCollection;
                        for (int i = 0; i < properties.Count; i++)
                        {
                            DynamicBlockReferenceProperty property = properties[i];
                            if (property.PropertyName == "par_l")
                            {
                                ed.WriteMessage(property.Value+"");
                                property.Value = 500.0;
                            }
                        }
                       tr.Commit();  
            }
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   gileCAD    8 年前

    您必须将新创建的块引用附加到某个BlockTableRecord,并将其添加到活动事务中。

    [CommandMethod("TEST")]
    public void Test()
    {
        var doc = AcAp.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;
    
        using (var tr = db.TransactionManager.StartTransaction())
        {
            var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
            // if the bloc table has the block definition
            if (bt.Has("prz_podl"))
            {
                // create a new block reference
                var br = new BlockReference(Point3d.Origin, bt["prz_podl"]);
    
                // add the block reference to the curentSpace and the transaction
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
    
                // set the dynamic property value
                foreach (DynamicBlockReferenceProperty prop in br.DynamicBlockReferencePropertyCollection)
                {
                    if (prop.PropertyName == "par_l")
                    {
                        prop.Value = 500.0;
                    }
                }
            } 
            // save changes
            tr.Commit();
        } // <- end using: disposing the transaction and all objects opened with it (block table) or added to it (block reference)
    }