创建视口
public void MakeVport()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Entity MsClipBorder;
Entity PsClipBorder = new acDbSvs.Polyline();
acDbSvs.Viewport Vp = new acDbSvs.Viewport();
Point3d centerPoint = Point3d.Origin;
Point3d SelPoint0 = Point3d.Origin;
Point3d SelPoint1 = Point3d.Origin;
ObjectId ClipObjectId = ObjectId.Null;
double Scale = 96.0;
object orthomode = Application.GetSystemVariable("orthomode");//保存系统变量
string DrawingSalePrexfix = "1:";//注释比例前缀
//ini文件名称
string IniFileName = Directory.GetParent(StaticFileSystem.GetCurrentPath()) + "\\sys\\arch_tcc.ini";
string defaultText = "1:";
//判断英制还是公制
if (File.Exists(IniFileName))
{
StringBuilder temp = new StringBuilder(1024);
StaticFileSystem.GetPrivateProfileString("绘图单位", "绘图单位", defaultText, temp, 1024, IniFileName);
if (temp.ToString() == "英制")
DrawingSalePrexfix = "['\"]";
else
DrawingSalePrexfix = "1:";
}
else
DrawingSalePrexfix = "1:";
#region
//LayoutManager lm =LayoutManager.Current;
//ObjectId cur_layoutid = lm.GetLayoutId(lm.CurrentLayout);
//string layoutname = LayoutManager.Current.CurrentLayout;
#endregion
//创建图层,设置颜色
layermanager.CreateLayer("viewport_border", 252);
layermanager.layerIsPlottable("viewport_border", false);
//切换到模型空间
object tilemode = Application.GetSystemVariable("tilemode");
if (System.Convert.ToInt16(tilemode) == 0)
Application.SetSystemVariable("TILEMODE", 1);
//选择视口角点
PromptPointOptions Ppo = new PromptPointOptions("\n选择视口第一角点,[右键选择多段线、椭圆、圆]");
Ppo.AllowNone = true;
PromptPointResult Ppr = ed.GetPoint(Ppo);
if (Ppr.Status == PromptStatus.Cancel)
{
PsClipBorder.Dispose();
Vp.Dispose();
return;
}
//用多段线、椭圆、圆作为视口边界
if (Ppr.Status == PromptStatus.None)
{
PromptEntityOptions peo = new PromptEntityOptions("\n选择视口裁切的多段线、椭圆、圆");
peo.SetRejectMessage("\n必须是多段线、圆、椭圆");
peo.AddAllowedClass(typeof(acDbSvs.Polyline), false);
peo.AddAllowedClass(typeof(acDbSvs.Circle), false);
peo.AddAllowedClass(typeof(acDbSvs.Ellipse), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
PsClipBorder.Dispose();
Vp.Dispose();
return;
}
ClipObjectId = per.ObjectId;
}
//选择视口另一角点
if (Ppr.Status == PromptStatus.OK)
{
SelPoint0 = Ppr.Value;
PromptCornerOptions Pco = new PromptCornerOptions("\n选择视口另一角点", SelPoint0);
PromptPointResult Pcr = ed.GetCorner(Pco);
if (Pcr.Status == PromptStatus.Cancel)
{
PsClipBorder.Dispose();
Vp.Dispose();
return;
}
SelPoint1 = Pcr.Value;
}
//视口比例
if (GetScale() == false)//按取消键,退出
{
PsClipBorder.Dispose();
Vp.Dispose();
return;
}
Scale = 1 / Scale;//注释比例,1/96
//切换至图纸空间
Application.SetSystemVariable("TILEMODE", 0);
doc.Editor.SwitchToPaperSpace();
//事务管理器tr
Transaction tr = db.TransactionManager.StartTransaction();
////!!!此处用openclosetransaction无法通过!!!
//OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction();
using (tr)
{
try
{
BlockTable tbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
// open 图纸空间块表
BlockTableRecord btr = tr.GetObject(tbl, OpenMode.ForWrite) as BlockTableRecord;
//选择图元作为视口裁切编辑
if (ClipObjectId != ObjectId.Null)
{
MsClipBorder = tr.GetObject(ClipObjectId, OpenMode.ForRead) as Entity;
PsClipBorder = MsClipBorder.Clone() as Entity;
}
//用户选择point,创建pline
else
{
acDbSvs.Polyline pl = new acDbSvs.Polyline();
pl.AddVertexAt(0, new Point2d(SelPoint0.X, SelPoint0.Y), 0, 0, 0);
pl.AddVertexAt(0, new Point2d(SelPoint1.X, SelPoint0.Y), 0, 0, 0);
pl.AddVertexAt(0, new Point2d(SelPoint1.X, SelPoint1.Y), 0, 0, 0);
pl.AddVertexAt(0, new Point2d(SelPoint0.X, SelPoint1.Y), 0, 0, 0);
PsClipBorder = pl as Entity;
}
//视口角点坐标
Point3d maxPoint = PsClipBorder.GeometricExtents.MaxPoint;
Point3d minPoint = PsClipBorder.GeometricExtents.MinPoint;
//视口中心点
centerPoint = new Point3d((maxPoint.X + minPoint.X) / 2, (maxPoint.Y + minPoint.Y) / 2, 0);
//多段线闭合
if (PsClipBorder is acDbSvs.Polyline)
{
acDbSvs.Polyline my_pl = PsClipBorder as acDbSvs.Polyline;
my_pl.Closed = true;
}
//改到viewport_border层,不打印图层
PsClipBorder.Layer = "viewport_border";
//视口边界缩放到图纸空间
PsClipBorder.TransformBy(Matrix3d.Scaling(Scale, centerPoint));
//添加多段线到数据库
btr.AppendEntity(PsClipBorder);
tr.AddNewlyCreatedDBObject(PsClipBorder, true);
//添加视口到数据库
//vp.Annotative = AnnotativeStates.True;
btr.AppendEntity(Vp);
tr.AddNewlyCreatedDBObject(Vp, true);
//设置视口参数
Vp.NonRectClipEntityId = PsClipBorder.Id;
var pt = new Point2d(centerPoint.X, centerPoint.Y);//视口在模型空间插入位置
Vp.ViewCenter = pt; //模型空间中心位置
Vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
Vp.CenterPoint = centerPoint;//视口插入位置
Vp.Height = Scale * Vp.ViewHeight;// 视口高度
//vp.CustomScale = Scale;
Vp.NonRectClipOn = true;
Vp.Locked = true;
//vp.Annotative = AnnotativeStates.True;//不能用
Vp.On = true;
//设置注释比例
ObjectContextManager ocm = db.ObjectContextManager;
ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
foreach (AnnotationScale objcon in occ) //AnnotationScale
{
if (objcon.Scale.Equals(Scale) && Regex.IsMatch(objcon.Name, DrawingSalePrexfix))
{
Vp.AnnotationScale = objcon;
//ed.WriteMessage("\n" + objcon.Name.ToString());
break;
}
}
tr.Commit();//提交更改
}// end of try
catch (System.Exception ex)
{
Vp.Dispose();
PsClipBorder.Dispose();
ed.WriteMessage("\ncatch" + ex.Message);
}
}// end of using tr
//*************Jig 拖动**************************
Application.SetSystemVariable("orthomode", 0);
DragDrawJig jig = new DragDrawJig(centerPoint);
using (jig)
{
try
{
jig.BasePnt = centerPoint;//拖动基点
jig.PrmpStr = "\n视口中心位置";//提示字符
//事务管理器
using (Transaction tr1 = db.TransactionManager.StartTransaction())
{
BlockTable tbl = tr1.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr1.GetObject(tbl, OpenMode.ForWrite) as BlockTableRecord;
#region
//foreach (ObjectId id in objectIds)
//{
// Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
// jig.AddEntity(ent);
//}
//Entity vp_entity = tr1.GetObject(vp.ObjectId, OpenMode.ForWrite) as Entity;
//Entity vp_entity = newpl.Clone() as Entity;
//btr.AppendEntity(vp_entity);
//tr.AddNewlyCreatedDBObject(vp_entity,false);e
#endregion
//以写方式打开视口裁切多段线
tr1.GetObject(PsClipBorder.ObjectId, OpenMode.ForWrite);
jig.AddEntity(PsClipBorder);
// Draw Jig 交互
PromptResult pr;
pr = doc.Editor.Drag(jig);
#region
//if (pr.Status == PromptStatus.Keyword)
//{
// doc.Editor.WriteMessage(pr.StringResult);
// doc.Editor.WriteMessage(pr.ToString());
//}
#endregion
// 结果
if (pr.Status == PromptStatus.OK)
{
//视口至鼠标选定位置
jig.TransFormEntities();
tr1.Commit();
doc.Editor.WriteMessage("\n视口已创建");
Application.SetSystemVariable("orthomode", orthomode);
return;
}
else
{
//删除新建视口
PsClipBorder.Erase();
tr1.Commit();
Application.SetSystemVariable("orthomode", orthomode);
doc.Editor.WriteMessage("\n用户取消!");
return;
}
}// end of using tr1
}//end of try
catch (System.Exception ex)
{
ed.WriteMessage("\nJig出错了");
ed.WriteMessage("\ncatch" + ex.Message);
Application.SetSystemVariable("orthomode", orthomode);
return;
}
}// end of using Jig
//输入视口比例函数
bool GetScale()
{
//Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptions pdo = new PromptDoubleOptions("\n输入视口比例");
pdo.AllowNegative = false;
pdo.AllowZero = false;
pdo.UseDefaultValue = true;
pdo.DefaultValue = 96.0;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if (pdr.Status == PromptStatus.Cancel)
{
//ed.WriteMessage("\ncancel");
return false;
}
if (pdr.Status == PromptStatus.OK)
{
Scale = pdr.Value;
return true;
}
else
{
Scale = pdo.DefaultValue;
return true;
}
}
}
赞https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif
页:
[1]