Pipe to Revit

Hi, I’m trying to create c# code, which one add Pipe to Revit. It works, but I don’t understand why coordinates are changed during this process. It’s look like scale from point, but I’dont do it in the code.

// Grasshopper Script Instance
#region Usings

#r "C:\ProgramData\Autodesk\Revit\Addins\2023\RhinoInside.Revit\R8\RhinoInside.Revit.dll"
#r "C:\Program Files\Autodesk\Revit 2023\RevitAPI.dll"
#r "C:\Program Files\Autodesk\Revit 2023\RevitAPIUI.dll"
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using RhinoInside.Revit;

using RhinoInside.Revit.UI;
using RhinoInside.Revit.Convert.Geometry;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

#endregion

public class Script_Instance : GH_ScriptInstance
{
        // active Revit version
    public static string REVIT_VERSION = Revit.ActiveUIApplication.Application.VersionNumber;

    // access the active document object
    private Autodesk.Revit.DB.Document doc = Revit.ActiveDBDocument;

    #region Notes
    /* 
      Members:
        RhinoDoc RhinoDocument
        GH_Document GrasshopperDocument
        IGH_Component Component
        int Iteration

      Methods (Virtual & overridable):
        Print(string text)
        Print(string format, params object[] args)
        Reflect(object obj)
        Reflect(object obj, string method_name)
    */
    #endregion

    private void RunScript(
	bool GO,
	List<object> Line,
	List<object> systemType,
	List<object> pipeType,
	object level,
	ref object pipeOut)
    {
         if (GO)
        {
            var lineList = new List<Rhino.Geometry.Line>();
            var systemTypeIdList = new List<Autodesk.Revit.DB.ElementId>();
            var pypeTypeIdList = new List<Autodesk.Revit.DB.ElementId>();
            var pypeList = new List<Autodesk.Revit.DB.Plumbing.Pipe>();

            var RevitLevel = (Autodesk.Revit.DB.Level)level;
            var RevitLevelId = RevitLevel.Id;

            foreach (var item in systemType)
            {
                var Revititem = (Autodesk.Revit.DB.Element)item;
                systemTypeIdList.Add(Revititem.Id); 
            }

            foreach (var item in pipeType)
            {
                var Revititem = (Autodesk.Revit.DB.Element)item;
                pypeTypeIdList.Add(Revititem.Id); 
            }

            foreach (var item in Line)
            {
                lineList.Add((Rhino.Geometry.Line)item);
            }
            
            var sP = new List<Autodesk.Revit.DB.XYZ>();
            var eP = new List<Autodesk.Revit.DB.XYZ>();
            
            for (int i = 0; i < lineList.Count; i++)
            {
                sP.Add(new Autodesk.Revit.DB.XYZ(lineList[i].FromX, lineList[i].FromY, lineList[i].FromZ));
                eP.Add(new Autodesk.Revit.DB.XYZ(lineList[i].ToX, lineList[i].ToY, lineList[i].ToZ));
            }

            
            // create and start the transaction
            using (Transaction t = new Transaction(doc, "CreatePipe"))
            {
                t.Start();

                for (int i = 0; i < systemTypeIdList.Count; i++)
                {
                    var Pipe = Autodesk.Revit.DB.Plumbing.Pipe.Create(
                        doc, 
                        systemTypeIdList[i], 
                        pypeTypeIdList[i], 
                        RevitLevelId,
                        sP[i], 
                        eP[i]);

                    pypeList.Add(Pipe);
                }
                
                // commit the changes after all changes has been made
                t.Commit();
            }
            pipeOut = pypeList;
        }
    }
}

In a picture you can see green - Revit elements after adding from GR, Red - input to node. Please, any ideas.


XmlToRevit_forum.gh (92.9 KB)

Basically I believe your issue is Revit’s internal units which is always in Imperial Feet. Now RiR have great Geometry conversion methods that you can utilize to do that for you and convert between Revit and Rhino geometry seamlessly… check the below and hope it works

This part of your code you can change to the below while using the GeometryEncoder as GE, the GE.ToXYZ() takes a Rhino Point and converts it to a revit DB.XYZ while also converting from Revit’s document Units to respective Feet units which the Revit API uses.

        using GE = RhinoInside.Revit.Convert.Geometry.GeometryEncoder;


        var sP = new List<Autodesk.Revit.DB.XYZ>();
        var eP = new List<Autodesk.Revit.DB.XYZ>();

        for (int i = 0; i < lineList.Count; i++)
        {
            sP.Add(GE.ToXYZ(lineList[i].From));
            eP.Add(GE.ToXYZ(lineList[i].To));
        }
1 Like

Thank you! Just 2 line and it works.