- 积分
- 1386
- 明经币
- 个
- 注册时间
- 2023-2-2
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2023-10-30 00:59:29
|
显示全部楼层
本帖最后由 箭头_Row 于 2023-12-12 03:11 编辑
文件一名稱:Viewport.cs
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- namespace ArrowTools
- {
- public static partial class Viewport
- {
- /// <summary>
- ///功能一:布局中选择视口将比例附着给标注。
- ///功能二:设置标注比例:(DIMSCALE:DIMLFAC=1:{acScale}。
- /// </summary>
- [CommandMethod("acViewportScaleToDim")]
- public static void acViewportScaleToDim()
- {
- // Get the current document and database
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // Get the current document editor
- Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
- ////定义当前活动Space
- //string acSpace;
- //acSpace = Tools.acSpaceName();
- //if (acSpace == "*Model_Space")
- //{
- // Tools.SetFirstLayoutAsCurrent();
- //}
- //else
- //{
- //}
- //定义当前活动acLayout
- string acLayout = Tools.acLayoutName();
- //判断活动空间是否为模型空间
- if (acLayout == "Model")
- {
- Tools.SetFirstLayoutAsCurrent();
- }
- else
- {
- //执行命令ps,将鼠标切换至视口外
- //acDocEd.Command("SPACE");
- acDocEd.SwitchToPaperSpace();//执行命令SwitchToPaperSpace(),将鼠标切换至视口外
- }
- //定义当前视绘图比例
- double acScale;
- acScale = Tools.acViewportCustomScale();
- //判断当前绘图比例的值
- if (acScale == 0)
- {
- //Application.ShowAlertDialog("必须选择视口,程序已中止!!!!");
- }
- else if (acScale % 5 == 0 || acScale == 1 || acScale == 2 || acScale == 3 || acScale == 5 || acScale == 6 || acScale == 8)
- {
- Tools.acScaleToDim(acScale);
- // Set system variable to new value
- Application.SetSystemVariable("DIMLFAC", acScale);
- Application.SetSystemVariable("DIMSCALE", 1);
- // 命令行提示
- Application.ShowAlertDialog($"标注比例已修改DIMSCALE:DIMLFAC = 1:{acScale})");
- }
- else
- {
- Application.ShowAlertDialog("视口比例错误(非整数),检查视口比例!!!!");
- }
- }
- }
- }
文件二名稱:Tools.cs
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using System;
- namespace ArrowTools
- {
- public static partial class Tools
- {
- /// <summary>
- /// 执行:拾起视口 "acScale"
- /// </summary>
- /// <returns></returns>
- public static double acViewportCustomScale()
- {
- double acScale; //定义当前视口比例
- acScale = 0;
- // Get the current document and database
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // Get the current document
- Editor acDocEd = acDoc.Editor;
- PromptEntityResult acSSPrompt = acDocEd.GetEntity("选择视口");
- //判断是否选择了图元
- if (acSSPrompt.Status == PromptStatus.OK)
- {
- ObjectId acSSet = acSSPrompt.ObjectId;
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- Entity acEnt = acTrans.GetObject(acSSet, OpenMode.ForRead) as Entity;
- //判断选择的对象是否为视口
- if (acEnt.GetType().Name == "Viewport")
- {
- Autodesk.AutoCAD.DatabaseServices.Viewport acVport;
- acVport = acTrans.GetObject(acEnt.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Viewport;
- //定义当前视口比例
- double UnroundedacScale;
- UnroundedacScale = 1 / acVport.CustomScale;
- //取整当前视口比例,因为取倒数后计算存在偏差。
- acScale = Math.Round(UnroundedacScale, 10);
- Application.ShowAlertDialog("视口比例: " + acScale + " "
- + "是否锁定: " + acVport.Locked);
- }
- else
- {
- Application.ShowAlertDialog("必须选择视口,程序已经中止!!!!");
- }
- return acScale;
- }
- }
- else
- {
- Application.ShowAlertDialog("必须选择视口!");
- return acScale;
- }
- }
- /// <summary>
- /// 输入:视口比例
- /// 执行:框选对象,将"acScale"附加给标注
- /// </summary>
- /// <param name="acScale"></param>
- public static void acScaleToDim(double acScale)
- {
- // Get the current document and database
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // Get the current document
- Editor acDocEd = acDoc.Editor;
- // Create a TypedValue array to define the filter criteria
- TypedValue[] acTypValAr = new TypedValue[1];
- acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "DIMENSION"), 0);
- // Assign the filter criteria to a SelectionFilter object
- SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
- // 创建一个选择选项对象
- PromptSelectionOptions acPSOptions = new PromptSelectionOptions();
- acPSOptions.MessageForAdding = "选择对象(过滤条件:标注):";
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- // 调用GetSelection方法,并传递选择选项参数
- PromptSelectionResult acSSPrompt;
- //acSSPrompt = acDocEd.GetSelection(acSelFtr);
- acSSPrompt = acDocEd.GetSelection(acPSOptions, acSelFtr);
- // If the prompt status is OK, objects were selected
- if (acSSPrompt.Status == PromptStatus.OK)
- {
- SelectionSet acSSet = acSSPrompt.Value;
- Application.ShowAlertDialog("已修改标注属性数量: " +
- acSSet.Count.ToString());
- // Step through the objects in the selection set
- foreach (SelectedObject acSSObj in acSSet)
- {
- // Check to make sure a valid SelectedObject object was returned
- if (acSSObj != null)
- {
- // Open the selected object for write
- Dimension acDim;
- acDim = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Dimension;
- //Application.ShowAlertDialog("标注比例: " + acDim.Dimscale + "\n"
- // + "标注线形比例: " + acDim.Dimlfac + "\n"
- // + "标注样式: " + acDim.DimensionStyleName + "\n" /* + "+" + acDim.Annotative + "+" + acDim.IsModified */
- // + "标注图层和颜色: " + acDim.Layer.ToString() + "+" + acDim.ColorIndex.ToString()
- // );
- // 修改选择对象的属性:颜色、标注比例、线形比例
- acDim.ColorIndex = 0;
- acDim.Dimscale = 1;
- acDim.Dimlfac = acScale;
- }
- }
- // Save the new object to the database
- acTrans.Commit();
- }
- else
- {
- Application.ShowAlertDialog("已修改标注属性数量: 0");
- }
- }
- }
- /// <summary>
- /// 功能:活动空间切换至图纸的第一个布局空间
- /// </summary>
- public static void SetFirstLayoutAsCurrent()
- {
- // 获取当前文档和数据库
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // Get the current document
- Editor acDocEd = acDoc.Editor;
- // 启动事务
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- // 获取布局字典
- DBDictionary layoutDic = acTrans.GetObject(acCurDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
- // 定义要切换的布局空间的名称
- string firstLayout = "";
- // 遍历布局字典,找到第一个非模型空间的布局
- foreach (DBDictionaryEntry entry in layoutDic)
- {
- if (entry.Key != "Model")
- {
- // 记录该布局的名称
- firstLayout = entry.Key;
- break;
- }
- }
- // 如果找到了第一个非模型空间的布局,则切换至该布局空间
- if (firstLayout != "")
- {
- // 获取布局管理器
- LayoutManager acSpace = LayoutManager.Current;
- // 切换至该布局空间
- acSpace.CurrentLayout = firstLayout;
- acDocEd.WriteMessage($"\n已切换至布局:{firstLayout}。");
- }
- else
- {
- // 提示用户不存在非模型空间的布局
- acDocEd.WriteMessage("\n当前图纸不存在非模型空间的布局,请先创建至少一个布局。");
- }
- //提交事务
- acTrans.Commit();
- }
- }
- /// <summary>
- /// 功能:读取当前空间名称
- /// </summary>
- /// <returns></returns>返回spaceName變量的值
- public static string acSpaceName() // 修改返回類型為string
- {
- // Get the current document and database
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // 獲取當前space的對象ID
- ObjectId acSpaceId = acCurDb.CurrentSpaceId;
- // 定義一個string變量,用於存儲space名稱
- string acSpaceName = "";
- // 開始一個事務
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- // Open the Block table record Model space for write
- BlockTableRecord acBlkTblRec;
- acBlkTblRec = acTrans.GetObject(acSpaceId,
- OpenMode.ForWrite) as BlockTableRecord;
- acSpaceName = acBlkTblRec.Name;
- }
- // 返回spaceName變量的值
- return acSpaceName;
- }
- /// <summary>
- /// 获取活动空间名称:布局或“Model”
- /// </summary>
- /// <returns></returns>活动空间名称
- public static string acLayoutName()
- {
- // 獲取佈局管理器實例
- LayoutManager acLM = LayoutManager.Current;
- //当前布局名称
- string acLayoutName = acLM.CurrentLayout;
- return acLayoutName;
- }
- /// <summary>
- /// 功能:先选择对象、再合并为多段线。
- /// </summary>
- /// <returns></returns>选择集
- public static SelectionSet SelectedJoinToPolylines()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- // Prompt user to select polylines
- PromptSelectionResult selectionResult = ed.GetSelection();
- // Create a selection set from the user's selection
- SelectionSet ss = selectionResult.Value;
- if (selectionResult.Status != PromptStatus.OK || ss.Count == 1)
- {
- return null;
- }
- else
- {
- // Execute the JOIN command on the selected polylines
- ed.Command("_JOIN", ss, "");
- ed.WriteMessage($"{ss.Count} 个对象已转换为多段线.");
- return ss;
- }
- }
- }
- }
|
|