块插入图中时,块随鼠标移动 EntityJig

代码
//继承EntityJig  两个函数重载   
 public class CADJig : EntityJig
    {
      //  houlinbo QQ420021327
       Entity moveEnt; 
//移动的实体
        Point3d ptTo;   //插入点
        string promp;   //提示
        public CADJig(Entity ent, string strPromp)
            : 
base(ent)
        {
            moveEnt 
= ent; //打开的 ent
            promp = strPromp;
        }


        
protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions optJigPoint 
= new JigPromptPointOptions(promp);
            
//optJigPoint.Cursor = CursorType.RubberBand;
            PromptPointResult resJigPoint = prompts.AcquirePoint(optJigPoint);
            Point3d curPt 
= resJigPoint.Value;

            
if (ptTo != curPt)
            {
                ptTo 
= curPt;
                
return SamplerStatus.OK;
            }
            
else
            {
                
return SamplerStatus.NoChange;
            }
        }


        
protected override bool Update()
        {
            
try
            {
                ((BlockReference)moveEnt).Position 
= ptTo;
            }
            
catch (System.Exception)
            {
                
return false;
            }
            
return true;
        }
     }
代码
        /// <summary>
        
/// 将文件以块的形式添加到图中  随鼠标移动 JIG
        
/// </summary>
        
/// <returns></returns>
        public static ObjectId AddBlockDwgJig(Database db, Editor ed, String sourceFileName, String newBlockName, Point3d po, double dAngle, string jigPrompt)
        {
           //  houlinbo  QQ420021327
            BlockReference bref 
= null;
            Transaction trans 
= db.TransactionManager.StartTransaction();
            
using (trans)
            {
                
try
                {
                    BlockTable bt 
= (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                    BlockTableRecord btr 
= (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    ObjectId bobj 
= new ObjectId();
                    
if (bt.Has(newBlockName))
                    {
                        bobj 
= bt[newBlockName];
                    }
                    
else
                    {
                        Database databaseFromFile 
= Getdb(sourceFileName, System.IO.FileShare.Read, true);
                        bobj 
= db.Insert(newBlockName, databaseFromFile, true);
                    }
                    bref 
= new BlockReference(po, bobj);
                    bref.Rotation 
= dAngle;
                    ObjectId blockobj 
= btr.AppendEntity(bref);
                    BlockTableRecord empBtr 
= (BlockTableRecord)trans.GetObject(bt[newBlockName], OpenMode.ForWrite);

                    
//用以下两句调用随鼠标移动的效果 在事务未知前调用,否则原点会出现一个块
                    CADJig cadJig = new CADJig(bref as Entity, jigPrompt);
                    PromptResult pr 
= ed.Drag(cadJig);


                    
foreach (ObjectId id in empBtr)
                    {
                        Entity ent 
= (Entity)trans.GetObject(id, OpenMode.ForRead, true);
                        
if (ent.GetType() == typeof(AttributeDefinition))
                        {
                            
//块属性
                            AttributeReference attRef = new AttributeReference();
                            AttributeDefinition attDef 
= (AttributeDefinition)ent;
                            attRef.SetPropertiesFrom(attDef);
                            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
                            bref.AttributeCollection.AppendAttribute(attRef);
                            trans.AddNewlyCreatedDBObject(attRef, 
true);
                        }
                    }
                    trans.AddNewlyCreatedDBObject(bref, 
true);
                    trans.Commit();
                }
                
catch (Exception ex)
                {
                    CE.Elec.Common.ShowMgr.SException(
"出错Insert ARX " + ex.Message);
                }
                
finally
                {
                    trans.Dispose();
                }
            }
            
return bref.ObjectId;
        }

 

 

posted @ 2009-12-16 21:14  houlinbo  阅读(1197)  评论(0编辑  收藏  举报