public void MyTxtExp()
        {
            AcadDocument acadDocument = doc.GetAcadDocument() as AcadDocument;
            AcadApplication acadApplication = Application.AcadApplication as AcadApplication;
            PromptPointResult ppr1 = ed.GetPoint("请选择第一个点");
            if (ppr1.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d p1 = ppr1.Value;
            PromptPointResult ppr2 = ed.GetCorner("请选择第二个点", p1);
            if (ppr2.Status != PromptStatus.OK)
            {
                return;
            }
            Point3d p2 = ppr2.Value;
            PromptSelectionResult psr = ed.SelectWindow(p1, p2);
            if (psr.Status != PromptStatus.OK)
            {
                return;
            }
            List<AcadEntity> objs = new List<AcadEntity>();
            List<Entity> entities = new List<Entity>();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in psr.Value.GetObjectIds())
                {
                    Entity obj = id.GetObject(OpenMode.ForRead) as Entity;
                    entities.Add(obj);
                    objs.Add(obj.AcadObject as AcadEntity);
                }
                trans.Commit();
            }
            Extents3d extents = Extents3dEx.Ex_GetEntsExtents3d(entities);
            AcadSelectionSet acadSelectionSet = null;
            acadSelectionSet = acadDocument.SelectionSets.Add("SS01");
            string fullFileName = acadDocument.FullName;
            string direc = Path.GetDirectoryName(fullFileName);
            string fileName = Path.GetFileNameWithoutExtension(fullFileName);

            if (acadSelectionSet != null)
            {
                acadSelectionSet.AddItems(objs.ToArray());
                acadDocument.Export(direc + "\\" + fileName, "wmf", acadSelectionSet);
            }
            string importFileName = direc + "\\" + fileName + ".wmf";
            double[] point = new double[] { 0, 0, 0 };
            acadDocument.Import(importFileName, point, 1);

            acadSelectionSet.Delete();

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Entity ent = Utils.EntLast().GetObject(OpenMode.ForWrite) as Entity;

                Point3d basePoint = ent.GeometricExtents.MaxPoint;
                Point3d targetPoint = extents.MaxPoint;

                double baseDis = ent.GeometricExtents.MinPoint.DistanceTo(basePoint);
                double targetDis = extents.MinPoint.DistanceTo(targetPoint);

                double scale = targetDis / baseDis;

                ent.Ex_Move(basePoint, targetPoint);
                ent.Ex_Scale(targetPoint, scale);

                DBObjectCollection dbObjs = new DBObjectCollection();
                ent.Explode(dbObjs);
                foreach (var obj in dbObjs)
                {
                    db.Ex_AddToModelSpace(obj as Entity);
                }
                ent.Erase(true);

                for (int i = 0; i < entities.Count; i++)
                {
                    ent = entities[i].ObjectId.GetObject(OpenMode.ForWrite) as Entity;
                    ent.Erase(true);
                }

                trans.Commit();
            }
        }

 

posted on 2023-11-17 09:09  HRDK  阅读(36)  评论(0编辑  收藏  举报