Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

An easy way to insert a fitting in a pipe?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
ankofl
261 Views, 3 Replies

An easy way to insert a fitting in a pipe?

Hi!

I have already read this topic  by @jeremy_tammik, but i still don't understand how to correctly insert the fitting into the pipe?
I wrote a small method, but its results are not what I expected:

 

 

public static void Split(Pipe pipe, out List<Pipe> splittedPipes, out List<FamilyInstance> placedFitings)
{
    splittedPipes = [];
    placedFitings = [];

    Document doc = pipe.Document;
    int numberOfSegments = 5; // Количество сегментов

    if (doc.GetSymbol("em_NVK_Футляр200", "Сталь", out FamilySymbol pipeAccessorySymbol))
    {
        // Получаем начальную и конечную точки трубы
        LocationCurve pipeLocation = pipe.Location as LocationCurve;
        XYZ startPoint = pipeLocation.Curve.GetEndPoint(0);
        XYZ endPoint = pipeLocation.Curve.GetEndPoint(1);

        // Вычисляем шаг между аксессуарами
        double pipeLength = pipeLocation.Curve.Length;
        double step = pipeLength / (numberOfSegments + 1);

        // Проходим по каждому участку и вставляем аксессуар
        for (int i = 1; i <= numberOfSegments; i++)
        {
            double distance = step * i;
            XYZ insertionPoint = startPoint + (endPoint - startPoint).Normalize() * distance;

            FamilyInstance accessory = doc.Create.NewFamilyInstance(insertionPoint, pipeAccessorySymbol, pipe, StructuralType.NonStructural);
        }
    }
}

 

 


What do I expect:

ankofl_0-1726060740835.png
What I get:
ankofl_1-1726060771921.png

 

I understand that it is possible to create separate pipes, taking into account the indentation of each inserted fitting, put these fittings on the plane of the pipe axis, then look for the matching connectors of the fittings and connectors of the pipe nearest to it, then connect all this, and then removing the original pipe, connect everything that was connected to the original pipe to the resulting fragments of new pipes...

But we can do all this literally in one click, just by clicking the fitting on the pipe, can we do this insertion through the API?

Thanks!

3 REPLIES 3
Message 2 of 4
jeremy_tammik
in reply to: ankofl

I am not a professional MEP programmer, and my experience in that area is limited. My main expl;oration into that area was the research and implementation creating a rolling offset:

  

  

Based on that, afaik, there are two ways to approach this:

  

  • Place the pipes only and connect them; fittings will be inserted according to the routing preferences
  • Place the fittings only and connect them; pipes will be automatically generated and inserted

  

For the situation you describe, the second option seems more appropriate.

  

Good luck, and looking forward to hearing how it goes.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 4
jeremy_tammik
in reply to: ankofl

Did you make any progress? How did you end up solving this?

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 4 of 4
ankofl
in reply to: jeremy_tammik

Hi!

I
just created pipe segments of a given length based on the old pipe, then deleted the old pipe, and connected all the segments Using standard Revit tools based on their tracing settings

 

doc.Create.NewUnionFitting(pair.One, pair.Two)

 

where pair is ConPair.cs

 

public class ConPair
{
	const double d = 0.001;

	public Connector One;
	public Connector Two;

	public bool Contain(List<ConPair> pairs)
	{
		foreach(ConPair pair in pairs)
		{
			if (One.Origin.DistanceTo(pair.One.Origin) < d &&
				Two.Origin.DistanceTo(pair.Two.Origin) < d)
			{
				return true;
			}
			else if (One.Origin.DistanceTo(pair.One.Origin) < d &&
				Two.Origin.DistanceTo(pair.Two.Origin) < d)
			{
				return true;
			}
		}
		return false;
	}

	public static bool GetPairs(List<Pipe> pipes, out List<ConPair> pairs)
	{
		pairs = [];  // Инициализация списка пар

		// Проходим по каждой трубе
		foreach (var pipe1 in pipes)
		{
			// Получаем все коннекторы первой трубы
			var connectors1 = pipe1.ConnectorManager.Connectors.Cast<Connector>().ToList();

			// Сравниваем её коннекторы с коннекторами остальных труб
			foreach (var pipe2 in pipes)
			{
				if (pipe1.Id == pipe2.Id) continue;  // Пропускаем ту же самую трубу

				// Получаем все коннекторы второй трубы
				var connectors2 = pipe2.ConnectorManager.Connectors.Cast<Connector>().ToList();

				// Сравниваем все коннекторы между двумя трубами
				foreach (var con1 in connectors1)
				{
					foreach (var con2 in connectors2)
					{
						// Вычисляем расстояние между коннекторами
						// Если расстояние меньше заданного d, создаем пару
						if (con1.Origin.DistanceTo(con2.Origin) < d)
						{
							ConPair pair = new() { One = con1, Two = con2 };
							if (!pair.Contain(pairs))
							{
								pairs.Add(pair);
							}								
						}
					}
				}
			}
		}

		// Возвращаем true, если была образована хотя бы одна пара
		return pairs.Count > 0;
	}

	public static bool GetPairs(Pipe pipe, FamilyInstance fitting, out ConPair pair)
	{
		pair = null;  // Инициализируем пару как null

		// Получаем коннекторы трубы
		var pipeConnectors = pipe.ConnectorManager.Connectors.Cast<Connector>().ToList();

		// Получаем коннекторы фитинга
		var fittingConnectors = fitting.MEPModel.ConnectorManager.Connectors.Cast<Connector>().ToList();

		// Проходим по каждому коннектору трубы
		foreach (var pipeConnector in pipeConnectors)
		{
			// Проходим по каждому коннектору фитинга
			foreach (var fittingConnector in fittingConnectors)
			{
				// Проверяем, соединены ли коннекторы напрямую
				if (pipeConnector.Origin.DistanceTo(fittingConnector.Origin) < d)
				{
					// Создаем пару коннекторов
					pair = new ConPair { One = pipeConnector, Two = fittingConnector };

					// Возвращаем true, если пара создана
					return true;
				}
			}
		}

		// Возвращаем false, если пара не была найдена
		return false;
	}
}

 


It's just that at the moment when I decided this at home (I think I marked your answer as a solution then), it seemed to me that my code (ConPair.cs) was one of many similar Utils classes that had already been here, and it didn't make sense to publish it then.

But here he is))

Thank you very much for your help in finding the method:

 

doc.Create.NewUnionFitting()

 

and in general solving this issue!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report