Revit二次开发——链接模型坐标系与模型坐标系转换

如果链接模型使用的是默认的【自动 - 原点到原点】
则转换前后坐标是一致的,否则,坐标是不一致的
在这里插入图片描述

下面是对链接文件中的管道的两个端点的坐标的转换:

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using System.Windows.Forms;
using Autodesk.Revit.DB.Plumbing;

namespace TransformCoordinate
{
    [Transaction(TransactionMode.Manual)]
    public class TransforCoor : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                UIDocument uiDoc = commandData.Application.ActiveUIDocument;
                Document doc = uiDoc.Document;

                Reference reference = uiDoc.Selection.PickObject(ObjectType.LinkedElement, new SelectionLinkFilter(), "select link Pipe.");

                Transform transform = null;
                if (reference != null)
                {
                    Element elemSelect = doc.GetElement(reference);
                    RevitLinkInstance linkInstance = elemSelect as RevitLinkInstance;
                    transform = linkInstance.GetTransform();

                    Document linkDoc = linkInstance.GetLinkDocument();
                    Element linkPipe = linkDoc.GetElement(reference.LinkedElementId);
                    if(linkPipe == null)
                    {
                        MessageBox.Show("linkPipe IS NULL");
                        return Result.Failed;
                    }
                    if (linkPipe.Location as LocationCurve == null)
                    {
                        MessageBox.Show("pipe line IS NULL");
                        return Result.Failed;
                    }
                    Line line = (linkPipe.Location as LocationCurve).Curve as Line;
                    XYZ point1 = line.GetEndPoint(0);
                    XYZ point2 = line.GetEndPoint(1);
                    
                    XYZ tPoint1 = transform.OfPoint(point1);
                    XYZ tPoint2 = transform.OfPoint(point2);

                    MessageBox.Show("Point1:" + point1.ToString() + Environment.NewLine + "tPoint1:" + tPoint1.ToString());
                    MessageBox.Show("Point2:" + point2.ToString() + Environment.NewLine + "tPoint2:" + tPoint2.ToString());
                }

                return Result.Succeeded;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Exception");
                return Result.Failed;
            }
        }
    }

    public class SelectionLinkFilter : ISelectionFilter
    {
        public RevitLinkInstance rvtIns;
        Document linkDoc = null;

        public bool AllowElement(Element elem)
        {
            if (elem is RevitLinkInstance){
            	rvtIns = elem as RevitLinkInstance;
                linkDoc = rvtIns.GetLinkDocument();
                return true;
            }
            return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            if (linkDoc.GetElement(reference.LinkedElementId) is Pipe)
            {
                return true;
            }
            return false;
        }
    }
}

详细参照:https://blog.csdn.net/Dai_Aixy/article/details/103150898?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-103150898-blog-124303379.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=2

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值