乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 110|回复: 8

X剪辑和块插入点

[复制链接]

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2011-9-20 14:36:07 | 显示全部楼层 |阅读模式
Kean在这里有一个很好的xclip工作示例http://through-the-interface.typepad.com/through_the_interface/2010/11/adding-a-2d-spatial-filter-to-perform-a-simple-xclip-on-an-external-reference-in-autocad-using-net.html
然而,这对我没有帮助,因为我正在尝试更新现有的剪辑信息
下面是一个工作函数,用于更改块的插入,在获得xclipped blockref之前,该函数工作正常
如果在XCLIPFRAME设置为2或1且图形中有剪裁的blockref的情况下运行以下代码,您将看到剪辑移动到LALALand中的某个位置
代码只是复制现有剪辑信息,然后以某种愚蠢的形式将其恢复。有人知道它在做什么吗
  1.         [CommandMethod("ChangeInsertionpoint")]
  2.         public static void NewInsertionPoint()
  3.         {
  4.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;
  7.             //Get the blockreference
  8.             PromptEntityOptions peo = new PromptEntityOptions("Pick a blockreference to change the insertion point:");
  9.             peo.SetRejectMessage("Must be a blockreference:");
  10.             peo.AddAllowedClass (typeof(BlockReference),true);
  11.             PromptEntityResult per = ed.GetEntity(peo);
  12.             if (per.Status != PromptStatus.OK) return;
  13.             //New insertion point
  14.             PromptPointResult ppo=ed.GetPoint("Pick the new insertion point:");
  15.             if (ppo.Status != PromptStatus.OK) return;
  16.             Matrix3d ucs=ed.CurrentUserCoordinateSystem;
  17.             Point3d insPt = ppo.Value.TransformBy(ucs),newPt;
  18.             bool hasAtts=false;
  19.             
  20.             using(Transaction tr = db.TransactionManager.StartTransaction())        
  21.             {
  22.                 BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
  23.                 ObjectId blockId=br.BlockTableRecord;
  24.                 BlockTableRecord btr = tr.GetObject(blockId, OpenMode.ForWrite) as BlockTableRecord;
  25.                 Plane plane=new Plane(ucs.CoordinateSystem3d.Origin,ucs.CoordinateSystem3d.Zaxis);
  26.                 //Transform inspt back to the blockdef
  27.                 Matrix3d blockmatrix = br.BlockTransform;
  28.                 insPt= insPt.TransformBy(blockmatrix.Inverse());
  29.                 //Util.AddEnt.Point(insPt, 2);
  30.                 Vector3d v=btr.Origin-insPt;
  31.                 Matrix3d move=Matrix3d.Displacement(v);
  32.                 Matrix2d disp = Matrix2d.Displacement(insPt.Convert2d(plane)-btr.Origin.Convert2d(plane));
  33.       
  34.                 //Move the ents in the block
  35.                 foreach (ObjectId id in btr)
  36.                 {
  37.                     Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  38.                     ent.TransformBy(move);
  39.                     ent.DowngradeOpen();
  40.                     if(ent.GetType()==typeof(AttributeDefinition))
  41.                         hasAtts=true;
  42.                 }
  43.                 ObjectIdCollection ids = btr.GetBlockReferenceIds(false, true);
  44.                 //Move the BlockReferences
  45.                 foreach (ObjectId brefId in ids)
  46.                 {
  47.                     bool btrans = false;
  48.                     BlockReference b = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
  49.                     if (b.BlockTableRecord == blockId)
  50.                     {
  51.                         newPt = insPt.TransformBy(b.BlockTransform);
  52.                         b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
  53.                         btrans = true;
  54.                     }
  55.                     else
  56.                     {
  57.                         BlockTableRecord nestedBref = tr.GetObject(b.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
  58.                         foreach (ObjectId id in nestedBref)
  59.                         {
  60.                             if (id == brefId)
  61.                             {
  62.                                 newPt = insPt.TransformBy(b.BlockTransform);
  63.                                 b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
  64.                                 btrans = true;
  65.                                 break;
  66.                             }
  67.                         }
  68.                     }
  69.                     if (btrans)
  70.                     {
  71.                         ObjectId id = b.ExtensionDictionary;
  72.                         if (id == ObjectId.Null) continue;
  73.                         DBDictionary dic = tr.GetObject(id, OpenMode.ForWrite) as DBDictionary;
  74.                         const string spatialName = "SPATIAL";
  75.                         DBDictionary FilterDic = tr.GetObject(dic.GetAt("ACAD_FILTER"), OpenMode.ForWrite) as DBDictionary;
  76.                         if (!FilterDic.Contains(spatialName)) continue;
  77.                         SpatialFilter sf = tr.GetObject(FilterDic.GetAt(spatialName), OpenMode.ForWrite) as SpatialFilter;
  78.                         SpatialFilterDefinition def = sf.Definition;
  79.                         Point2dCollection pts = new Point2dCollection();
  80.                         foreach (Point2d pt in def.GetPoints())
  81.                         {
  82.                            //pts.Add(pt.TransformBy(disp));
  83.                             pts.Add(pt);
  84.                         }
  85.                         SpatialFilterDefinition NewDef = new SpatialFilterDefinition
  86.                             (def.GetPoints(), def.Normal, def.Elevation, def.FrontClip, def.BackClip, def.Enabled);
  87.                         //SpatialFilterDefinition NewDef = new SpatialFilterDefinition
  88.                         //    (pts, def.Normal, def.Elevation, def.FrontClip, def.BackClip, def.Enabled);
  89.                         SpatialFilter NewSf = new SpatialFilter();
  90.                         NewSf.Definition = NewDef;
  91.                         FilterDic.Remove(spatialName);
  92.                         FilterDic.SetAt(spatialName, NewSf);
  93.                         tr.AddNewlyCreatedDBObject(NewSf, true);
  94.                     }
  95.                 }
  96.                 if (hasAtts)
  97.                     Blocks.blocks.UpdateAtts(blockId, db);
  98.               tr.Commit();        
  99.             }  
  100.             
  101.         } // end NewInsertionPoint

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2011-9-20 23:23:40 | 显示全部楼层
再近一点,您必须减去块引用位置。
foreach (Point2d pt in def.GetPoints())
{
pts.地址(pt);
}

为 Vector3d v3 = br。Position.GetAsVector();
foreach (Point2d pt in def.GetPoints())
{
Point3d p = new Point3d(pt.X,pt.是的,防守海拔);
p = p -v3;
分。Add(p.Convert2d(plane));
}
但是,如果您复制剪辑的块引用,则会导致所有剪辑和块引用都转到剪辑的 forst 实例
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 2011-9-21 06:11:40 | 显示全部楼层
追踪布莱斯看起来是个大问题。
...希望我有时间玩
可能值得尝试调整斯蒂芬普雷斯顿或基恩的兴趣...
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2011-9-21 07:34:49 | 显示全部楼层
感谢您的回复克里,您的建议听起来不错。
我把它分成了2个函数
  1.         [CommandMethod("ChangeInsertionpoint")]
  2.         public static void NewInsertionPoint()
  3.         {
  4.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;
  7.             PromptEntityOptions peo = new PromptEntityOptions("Pick a blockreference to change the insertion point:");
  8.             peo.SetRejectMessage("Must be a blockreference:");
  9.             peo.AddAllowedClass (typeof(BlockReference),true);
  10.             PromptEntityResult per = ed.GetEntity(peo);
  11.             if (per.Status != PromptStatus.OK) return;
  12.             PromptPointResult ppo=ed.GetPoint("Pick the new insertion point:");
  13.             if (ppo.Status != PromptStatus.OK) return;
  14.             Matrix3d ucs=ed.CurrentUserCoordinateSystem;
  15.             Point3d insPt = ppo.Value.TransformBy(ucs),newPt;
  16.            
  17.             using(Transaction tr = db.TransactionManager.StartTransaction())        
  18.             {
  19.                 BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
  20.                 ObjectId blockId=br.BlockTableRecord;
  21.                 BlockTableRecord btr = tr.GetObject(blockId, OpenMode.ForWrite) as BlockTableRecord;
  22.                 Matrix3d blockmatrix = br.BlockTransform;
  23.                 insPt= insPt.TransformBy(blockmatrix.Inverse());
  24.                 Vector3d v=btr.Origin-insPt;
  25.                 Matrix3d move=Matrix3d.Displacement(v);
  26.                 foreach (ObjectId id in btr)
  27.                 {
  28.                     Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  29.                     ent.TransformBy(move);
  30.                     ent.DowngradeOpen();
  31.                 }
  32.                 ObjectIdCollection ids = btr.GetBlockReferenceIds(false, true);
  33.                 foreach (ObjectId brefId in ids)
  34.                 {
  35.                     BlockReference b = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
  36.                     newPt = insPt.TransformBy(b.BlockTransform);
  37.                     if (b.BlockTableRecord == blockId)
  38.                     {   
  39.                         b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
  40.                     }
  41.                     else
  42.                     {
  43.                         BlockTableRecord nestedBref = tr.GetObject(b.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
  44.                         foreach (ObjectId id in nestedBref)
  45.                         {
  46.                             if (id == brefId)
  47.                             {
  48.                                 b.TransformBy(Matrix3d.Displacement(newPt - b.Position));
  49.                                 break;
  50.                             }
  51.                         }
  52.                     }
  53.                     UpdateClip(db, b, v);
  54.                 }
  55.               tr.Commit();        
  56.             }
  57.             ed.Regen();
  58.         } // end NewInsertionPoint
  59.         public static void UpdateClip(Database db,BlockReference br,Vector3d v)
  60.         {
  61.             const string spatialName = "SPATIAL";
  62.             ObjectId ExId = br.ExtensionDictionary;
  63.             if (ExId == ObjectId.Null) return;
  64.             using (Transaction tr = db.TransactionManager.StartTransaction())
  65.             {
  66.                 DBDictionary dic = tr.GetObject(ExId, OpenMode.ForWrite) as DBDictionary;
  67.                 if (!dic.Contains("ACAD_FILTER")) return;
  68.                 DBDictionary clipDic = tr.GetObject(dic.GetAt("ACAD_FILTER"), OpenMode.ForWrite) as DBDictionary;
  69.                 if (!clipDic.Contains(spatialName)) return;
  70.                 SpatialFilter sf = tr.GetObject(clipDic.GetAt(spatialName), OpenMode.ForWrite) as SpatialFilter;
  71.                 SpatialFilterDefinition def = sf.Definition;
  72.                 Plane plane=new Plane(new Point3d(0,0,def.Elevation),def.Normal);
  73.                 Matrix3d m = br.BlockTransform;
  74.                 Matrix3d disp=Matrix3d.Displacement(v.Negate());
  75.                 Point2dCollection pts = new Point2dCollection();
  76.                 foreach (Point2d pt in def.GetPoints())
  77.                 {           
  78.                     Point3d p = new Point3d(pt.X, pt.Y,def.Elevation);
  79.                     p = p.TransformBy(m.Inverse());
  80.                     pts.Add(p.Convert2d(plane));
  81.                 }
  82.                 SpatialFilterDefinition def2=new SpatialFilterDefinition(
  83.                     pts,def.Normal,def.Elevation,def.FrontClip,def.BackClip,def.Enabled);
  84.                
  85.                 clipDic.Remove(spatialName);
  86.                 SpatialFilter sf2 = new SpatialFilter();
  87.                 sf2.Definition = def2;
  88.                 SpatialFilterVolume sfv=sf.GetVolume();
  89.                 clipDic.SetAt(spatialName, sf2);
  90.                 tr.AddNewlyCreatedDBObject(sf2,true);
  91.                
  92.                 tr.Commit();
  93.             }
  94.         } // end UpdateClip

,这是大多数情况下第一次工作,但第二次它抛出一个摆动器。此外,如果你剪辑一个块引用,然后旋转它,它会出错,所以复合矩阵需要退后一步。一开始,我认为我必须相对于新的插入点移动剪辑,但我没有。
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2014-10-28 16:48:36 | 显示全部楼层
我在2015年用VS2012 express和crash再次尝试了这一点,事实上,运行Kean的代码也会崩溃。我希望有人能在2015年尝试基恩代码
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2015-3-17 17:24:38 | 显示全部楼层
http://through-the-interface.typepad.com/through_the_interface/2010/11/adding-a-2d-spatial-filter-to-perform-a-simple-xclip-on-an-external-reference-in-autocad-using-net.html
以防万一其他人对剪辑感兴趣。这崩溃为我 ACAD 2015 机械使用香草. 基恩说他在2015年尝试过,它没有崩溃,所以我有点迷茫。希望别人可以试一试
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2015-3-18 04:09:58 | 显示全部楼层
我可以让 Kean 和此示例 http://adndevblog.typepad.com/autocad/2013/01/spatial-filter-xclip-command-command-in-c.html 将剪辑添加到外部参照。
但是,如果它不会反转,并且如果使用握把编辑剪辑,它就会崩溃。
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2015-3-18 13:32:29 | 显示全部楼层
谢谢你的努力,杰夫,我想我会放弃的。如果我单步执行代码,它不会崩溃,但我也不能编辑剪辑,有时我可以删除剪辑,但不能重新剪辑。
较新的代码看起来需要用较新的cad版本更新,所以我可能也会跳过这一步。
可惜,否则更改插入点代码真的很方便。
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2016-4-29 19:44:11 | 显示全部楼层
这似乎工作正常。
它不允许剪辑与blockref在不同的平面上。
在保存关闭并重新打开之前,不会显示剪辑夹点。
  1.         [CommandMethod("ChangeInsertionpoint")]
  2.         public static void NewInsertionPoint()
  3.         {
  4.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;
  7.             //Get the blockreference
  8.             PromptEntityOptions peo = new PromptEntityOptions("\nPick a blockreference to change the insertion point:");
  9.             peo.SetRejectMessage("Must be a blockreference:");
  10.             peo.AddAllowedClass (typeof(BlockReference),true);
  11.             PromptEntityResult per = ed.GetEntity(peo);
  12.             if (per.Status != PromptStatus.OK) return;
  13.             //New insertion point
  14.             PromptPointResult ppo=ed.GetPoint("\nPick the new insertion point:");
  15.             if (ppo.Status != PromptStatus.OK) return;
  16.             Matrix3d ucs=ed.CurrentUserCoordinateSystem;
  17.             Point3d insPt = ppo.Value.TransformBy(ucs),newPt=Point3d.Origin;
  18.             bool hasAtts=false;
  19.             
  20.             using(Transaction tr = db.TransactionManager.StartTransaction())        
  21.             {
  22.                 //Get the blockref
  23.                 BlockReference br = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as BlockReference;
  24.                 ObjectId blockId=br.BlockTableRecord;
  25.                 //get it's blockdef
  26.                 BlockTableRecord btr = tr.GetObject(blockId, OpenMode.ForWrite) as BlockTableRecord;
  27.    
  28.                 //Transform inspt back to the blockdef
  29.                 Matrix3d blockmatrix = br.BlockTransform;
  30.                 insPt= insPt.TransformBy(blockmatrix.Inverse());
  31.                 Matrix3d move = Matrix3d.Displacement(btr.Origin - insPt);
  32.       
  33.                 //Move the ents in the block
  34.                 foreach (ObjectId id in btr)
  35.                 {
  36.                     Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  37.                     ent.TransformBy(move);
  38.                     if(ent.GetType()==typeof(AttributeDefinition))
  39.                         hasAtts=true;
  40.                 }
  41.                 ObjectIdCollection ids = btr.GetBlockReferenceIds(false, true);
  42.                 //Move the BlockReferences
  43.                 Matrix3d mBref = new Matrix3d();
  44.                
  45.                 foreach (ObjectId brefId in ids)
  46.                 {
  47.                     bool btrans = false;
  48.                     BlockReference b = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
  49.                     Point3d bposition = b.Position;
  50.                     if (b.BlockTableRecord == blockId)//has to unless nested
  51.                     {
  52.                         newPt = insPt.TransformBy(b.BlockTransform);
  53.                         mBref=Matrix3d.Displacement(newPt - b.Position);
  54.                         b.TransformBy(mBref);
  55.                         btrans = true;
  56.                     }
  57.                     else
  58.                     {
  59.                         BlockTableRecord nestedBref = tr.GetObject(b.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
  60.                         foreach (ObjectId id in nestedBref)
  61.                         {
  62.                             if (id == brefId)
  63.                             {
  64.                                 newPt = insPt.TransformBy(b.BlockTransform);
  65.                                 b.TransformBy(mBref);
  66.                                 btrans = true;
  67.                                 break;
  68.                             }
  69.                         }
  70.                     }
  71.                     if (btrans)
  72.                     {
  73.                         ObjectId id = b.ExtensionDictionary;
  74.                         if (id == ObjectId.Null) continue;
  75.                         DBDictionary dic = tr.GetObject(id, OpenMode.ForWrite) as DBDictionary;
  76.                         const string spatialName = "SPATIAL";
  77.                         if (!dic.Contains("ACAD_FILTER")) continue;
  78.                         DBDictionary FilterDic = tr.GetObject(dic.GetAt("ACAD_FILTER"), OpenMode.ForWrite) as DBDictionary;
  79.                         if (!FilterDic.Contains(spatialName)) continue;
  80.                         SpatialFilter sf = tr.GetObject(FilterDic.GetAt(spatialName), OpenMode.ForWrite) as SpatialFilter;
  81.                         SpatialFilterDefinition def = sf.Definition;
  82.                         Point2dCollection pts = new Point2dCollection();
  83.                         Matrix3d m = sf.OriginalInverseBlockTransform;
  84.                        // Matrix3d w= sf.ClipSpaceToWorldCoordinateSystemTransform;
  85.                         //need this for clips on a different plane than the blockref
  86.                     
  87.                         CoordinateSystem3d cs = m.CoordinateSystem3d;                     
  88.                         Plane plane = new Plane(cs.Origin, cs.Zaxis);     
  89.                         Matrix2d disp = Matrix2d.Displacement(newPt.Convert2d(plane) - b.Position.Convert2d(plane));
  90.                         foreach (Point2d pt in def.GetPoints())
  91.                         {
  92.                             Point3d l = new Point3d(plane, pt);
  93.                             l = l.TransformBy(move);
  94.                             l = l.TransformBy(m);
  95.                             pts.Add(l.Convert2d(plane));
  96.                         }
  97.                         pts.TrimToSize();
  98.                         SpatialFilterDefinition NewDef = new SpatialFilterDefinition
  99.                             (pts,Vector3d.ZAxis,0, def.FrontClip, def.BackClip, true);
  100.                         SpatialFilter NewSf = new SpatialFilter();
  101.                         NewSf.Definition = NewDef;
  102.                         
  103.                         FilterDic.Remove(spatialName);//REMOVE the old filter
  104.                         FilterDic.SetAt(spatialName, NewSf);
  105.                         tr.AddNewlyCreatedDBObject(NewSf, true);                       
  106.                    }
  107.                     if (hasAtts)
  108.                         Blocks.blocks.UpdateAtts(blockId, db);
  109.                 }
  110.                
  111.               tr.Commit();        
  112.             }
  113.             ed.Regen();
  114.         } // end NewInsertionPoint
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 18:54 , Processed in 0.157632 second(s), 70 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表