明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1462|回复: 0

【已解决】用户坐标系出现偏差如何解决?

[复制链接]
发表于 2012-10-11 15:28:16 | 显示全部楼层 |阅读模式
本帖最后由 白糖 于 2012-10-11 16:42 编辑
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Windows.Forms;

  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.EditorInput;
  10. using Autodesk.AutoCAD.Geometry;
  11. using Autodesk.AutoCAD.Runtime;

  12. namespace UCS
  13. {
  14.   /// <summary>
  15.   /// Description of UserControl1.
  16.   /// </summary>
  17.   public partial class UserControl1 : UserControl
  18.   {
  19.     public static int i = 1;
  20.     public static List<Point3d> pts = new List<Point3d>();

  21.     public UserControl1()
  22.     {
  23.       //
  24.       // The InitializeComponent() call is required for Windows Forms designer support.
  25.       //
  26.       InitializeComponent();
  27.       
  28.       //
  29.       // TODO: Add constructor code after the InitializeComponent() call.
  30.       //
  31.     }
  32.    
  33.     [CommandMethod("newUCS")]
  34.     public void NewUCS()
  35.     {
  36.       Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  37.       Database db = doc.Database;
  38.       Editor ed = doc.Editor;
  39.       Point3d breakPoint = new Point3d();
  40.       using (Transaction trans = db.TransactionManager.StartTransaction())
  41.       {
  42.         UcsTable ut = trans.GetObject(db.UcsTableId,OpenMode.ForRead) as UcsTable;
  43.         UcsTableRecord utr;
  44.         if (ut.Has("New_UCS") == false)
  45.         {
  46.           utr = new UcsTableRecord();
  47.           utr.Name = "New_UCS";
  48.           ut.UpgradeOpen();
  49.           ut.Add(utr);
  50.           trans.AddNewlyCreatedDBObject(utr,true);
  51.         }else
  52.         {
  53.           utr = trans.GetObject(ut["New_UCS"],OpenMode.ForWrite) as UcsTableRecord;
  54.         }
  55.         //指定原点
  56.         PromptPointOptions pPtOpts = new PromptPointOptions("\n指定原点");
  57.         PromptPointResult pPtRes = ed.GetPoint(pPtOpts);
  58.         if (pPtRes.Status == PromptStatus.OK)
  59.         {
  60.           utr.Origin = pPtRes.Value;
  61.           breakPoint = pPtRes.Value;
  62.         }
  63.         
  64.         //指定Y轴
  65.         pPtOpts = new PromptPointOptions("指定Y轴方向");
  66.         pPtRes = ed.GetPoint(pPtOpts);
  67.         if (pPtRes.Status == PromptStatus.OK)
  68.         {
  69.           Vector3d vec = pPtRes.Value - utr.Origin;
  70.           utr.YAxis = vec.GetNormal();
  71.         }
  72.         
  73.         //Y轴旋转90°为X轴
  74.         utr.XAxis = utr.YAxis.RotateBy(Math.PI*3/2,Vector3d.ZAxis);
  75.         
  76.         Matrix3d mt = Matrix3d.AlignCoordinateSystem(Point3d.Origin,Vector3d.XAxis,Vector3d.YAxis,Vector3d.ZAxis,utr.Origin,utr.XAxis,utr.YAxis,Vector3d.ZAxis);
  77.         ed.CurrentUserCoordinateSystem = mt;
  78.         trans.Commit();
  79.       }
  80.       AddPoint();
  81.     }
  82.    
  83.     // 添加点
  84.     [CommandMethod("addpoint")]
  85.     public void AddPoint()
  86.     {
  87.       Database db = HostApplicationServices.WorkingDatabase;
  88.       Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  89.       Point3d pt;
  90.       PromptPointResult pPtRes;
  91.       PromptPointOptions pPtOpts = new PromptPointOptions("");
  92.       pPtOpts.Message = "\n请选择起点";
  93.       pPtRes = ed.GetPoint(pPtOpts);
  94.       while (pPtRes.Status == PromptStatus.OK | pPtRes.Status != PromptStatus.Cancel)
  95.       {
  96.         pt = pPtRes.Value;
  97.         ed.WriteMessage("选点"+pt.ToString());
  98.         Vector3d vec = db.Ucsorg.GetAsVector();
  99.         AddPoint(pt+vec);
  100.         pts.Add(pt);
  101.         pPtOpts.Message = "\n请选择下一点[ESC退出:]";
  102.         pPtOpts.UseBasePoint = true;
  103.         pPtOpts.BasePoint = pPtRes.Value;
  104.         pPtRes = ed.GetPoint(pPtOpts);
  105.       }
  106.     }
  107.    
  108.     public static ObjectIdCollection AddPoint(Point3d position)
  109.     {
  110.       DBPoint ent1 = new DBPoint(position);
  111.       DBText ent2 = new DBText();
  112.       ent2.Position = new Point3d(position.X + 5, position.Y + 5, position.Z);
  113.       ent2.TextString = i.ToString();
  114.       ent2.Height = 5;
  115.       Circle ent3 = new Circle(position,Vector3d.ZAxis,5);
  116.       
  117.       Entity[] ents = new Entity[3];
  118.       ents[0] = ent1;
  119.       ents[1] = ent2;
  120.       ents[2] = ent3;
  121.       ObjectIdCollection entsId = AppendEntity(ents);
  122.       i++;
  123.       return entsId;
  124.     }
  125.    
  126.     public static ObjectIdCollection AppendEntity(params Entity[] ents)
  127.     {
  128.       Database db = HostApplicationServices.WorkingDatabase;
  129.       ObjectIdCollection objIds = new ObjectIdCollection();
  130.       using (Transaction trans = db.TransactionManager.StartTransaction())
  131.       {
  132.         BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  133.         BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  134.         foreach (var ent in ents)
  135.         {
  136.           ent.ColorIndex = 210;
  137.           objIds.Add(btr.AppendEntity(ent));
  138.           trans.AddNewlyCreatedDBObject(ent, true);
  139.         }
  140.         trans.Commit();
  141.         return objIds;
  142.       }
  143.     }
  144.   }
  145. }
转换坐标解决了~

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:48 , Processed in 0.156819 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表