Skip to content

Commit

Permalink
implemented MEP utility methods, connect rolling offset to neighbours
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Tammik committed Jan 13, 2014
1 parent 1e1f104 commit 67f1cf7
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 5 deletions.
5 changes: 3 additions & 2 deletions BuildingCoder/BuildingCoder/CmdMepElementShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,14 @@ static public MEPSystem ExtractMechanicalOrPipingSystem(
return system;
}

// copied from sdk - TraverseSystem example
//
// Copied from Revit SDK TraverseSystem example
//
// (C) Copyright 2003-2010 by Autodesk, Inc.
//
/// <summary>
/// Get the mechanical or piping system
/// from the connectors of selected element
/// from the connectors of selected element.
/// </summary>
/// <param name="connectors">Connectors of selected element</param>
/// <returns>The found mechanical or piping system</returns>
Expand Down
13 changes: 12 additions & 1 deletion BuildingCoder/BuildingCoder/CmdRollingOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ double remainingPipeLength

using( Transaction tx = new Transaction( doc ) )
{
Pipe pipe = null;

tx.Start( "Rolling Offset" );

if( _place_model_line )
Expand All @@ -295,7 +297,7 @@ double remainingPipeLength
}
else
{
Pipe pipe = pipes[0];
pipe = pipes[0];

if( _use_static_pipe_create )
{
Expand Down Expand Up @@ -348,6 +350,15 @@ PipeType pipe_type_standard
}
}

if( null != pipe )
{
// Connect rolling offset pipe segment
// with its neighbours

Util.Connect( q0, pipes[0], pipe );
Util.Connect( q1, pipe, pipes[1] );
}

tx.Commit();
}
return Result.Succeeded;
Expand Down
4 changes: 2 additions & 2 deletions BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion( "2014.0.106.2" )]
[assembly: AssemblyFileVersion( "2014.0.106.2" )]
[assembly: AssemblyVersion( "2014.0.106.3" )]
[assembly: AssemblyFileVersion( "2014.0.106.3" )]
142 changes: 142 additions & 0 deletions BuildingCoder/BuildingCoder/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,148 @@ FilteredElementCollector collector
return null;
}
#endregion // Element filtering

#region MEP utilities
/// <summary>
/// Return the given element's connector manager,
/// using either the family instance MEPModel or
/// directly from the MEPCurve connector manager
/// for ducts and pipes.
/// </summary>
static ConnectorManager GetConnectorManager(
Element e )
{
MEPCurve mc = e as MEPCurve;
FamilyInstance fi = e as FamilyInstance;

if( null == mc && null == fi )
{
throw new ArgumentException(
"Element is neither an MEP curve nor a fitting." );
}

return null == mc
? fi.MEPModel.ConnectorManager
: mc.ConnectorManager;
}

/// <summary>
/// Return the element's connector at the given
/// location, and its other connector as well,
/// in case there are exactly two of them.
/// </summary>
/// <param name="e">An element, e.g. duct, pipe or family instance</param>
/// <param name="location">The location of one of its connectors</param>
/// <param name="otherConnector">The other connector, in case there are just two of them</param>
/// <returns>The connector at the given location</returns>
static Connector GetConnectorAt(
Element e,
XYZ location,
out Connector otherConnector )
{
otherConnector = null;

Connector targetConnector = null;

ConnectorManager cm = GetConnectorManager( e );

bool hasTwoConnectors = 2 == cm.Connectors.Size;

foreach( Connector c in cm.Connectors )
{
if( c.Origin.IsAlmostEqualTo( location ) )
{
targetConnector = c;

if( !hasTwoConnectors )
{
break;
}
}
else if( hasTwoConnectors )
{
otherConnector = c;
}
}
return targetConnector;
}

/// <summary>
/// Return the connector in the set
/// closest to the given point.
/// </summary>
static Connector GetConnectorClosestTo(
ConnectorSet connectors,
XYZ p )
{
Connector targetConnector = null;
double minDist = double.MaxValue;

foreach( Connector c in connectors )
{
double d = c.Origin.DistanceTo( p );

if( d < minDist )
{
targetConnector = c;
minDist = d;
}
}
return targetConnector;
}

/// <summary>
/// Return the connector on the element
/// closest to the given point.
/// </summary>
static Connector GetConnectorClosestTo(
Element e,
XYZ p )
{
ConnectorManager cm = GetConnectorManager( e );

return null == cm
? null
: GetConnectorClosestTo( cm.Connectors, p );
}

/// <summary>
/// Connect two MEP elements at a given point p.
/// </summary>
/// <exception cref="ArgumentException">Thrown if
/// one of the given elements lacks connectors.
/// </exception>
public static void Connect(
XYZ p,
Element a,
Element b )
{
ConnectorManager cm = GetConnectorManager( a );

if( null == cm )
{
throw new ArgumentException(
"Element a has no connectors." );
}

Connector ca = GetConnectorClosestTo(
cm.Connectors, p );

cm = GetConnectorManager( b );

if( null == cm )
{
throw new ArgumentException(
"Element b has no connectors." );
}

Connector cb = GetConnectorClosestTo(
cm.Connectors, p );

ca.ConnectTo( cb );
//cb.ConnectTo( ca );
}
#endregion // MEP utilities
}

#region Extension Method Classes
Expand Down

0 comments on commit 67f1cf7

Please sign in to comment.