Skip to content

Commit

Permalink
replace explicit selection code in CmdRollingOffset by new JtPairPick…
Browse files Browse the repository at this point in the history
…er class
  • Loading branch information
Jeremy Tammik committed Nov 12, 2014
1 parent 482bddd commit 84f4748
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 108 deletions.
22 changes: 18 additions & 4 deletions BuildingCoder/BuildingCoder/CmdNewCrossFitting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ namespace BuildingCoder
[Transaction( TransactionMode.Manual )]
class CmdNewCrossFitting : IExternalCommand
{
/// <summary>
/// Allow selection of pipe elements only.
/// </summary>
class PipeElementSelectionFilter : ISelectionFilter
{
public bool AllowElement( Element e )
{
return e is Pipe;
}

public bool AllowReference( Reference r, XYZ p )
{
return true;
}
}

/// <summary>
/// Return the normalised direction of the given pipe.
/// </summary>
Expand Down Expand Up @@ -79,11 +95,9 @@ public Result Execute(
{
Selection sel = app.ActiveUIDocument.Selection;

ISelectionFilter filter = new CmdRollingOffset
.PipeElementSelectionFilter();

pipes = sel.PickElementsByRectangle(
filter, "Please pick some pipes." );
new PipeElementSelectionFilter(),
"Please pick some pipes." );
}
catch( Autodesk.Revit.Exceptions
.InvalidOperationException )
Expand Down
202 changes: 101 additions & 101 deletions BuildingCoder/BuildingCoder/CmdRollingOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ class CmdRollingOffset : IExternalCommand
const string _prompt
= "Please run this in a model containing "
+ "exactly two parallel offset pipe elements, "
+ "and they will be "
+ "automatically selected. Alternatively, pre-"
+ "select two pipe elements before launching "
+ "this command, or post-select them when "
+ "prompted.";
+ "and they will be automatically selected. "
+ "Alternatively, pre-select two pipe elements "
+ "before launching this command, or post-select "
+ "them when prompted.";

const BuiltInParameter bipDiameter
= BuiltInParameter.RBS_PIPE_DIAMETER_PARAM;
Expand All @@ -78,22 +77,6 @@ static double GetWallThickness( Pipe pipe )
}
#endregion // Determine Pipe Wall Thickness

/// <summary>
/// Allow selection of pipe elements only.
/// </summary>
public class PipeElementSelectionFilter : ISelectionFilter
{
public bool AllowElement( Element e )
{
return e is Pipe;
}

public bool AllowReference( Reference r, XYZ p )
{
return true;
}
}

public Result Execute(
ExternalCommandData commandData,
ref string message,
Expand All @@ -104,112 +87,129 @@ public Result Execute(
Application app = uiapp.Application;
Document doc = uidoc.Document;

// Select all pipes in the entire model.
//// Select all pipes in the entire model.

List<Pipe> pipes = new List<Pipe>(
new FilteredElementCollector( doc )
.OfClass( typeof( Pipe ) )
.ToElements()
.Cast<Pipe>() );
//List<Pipe> pipes = new List<Pipe>(
// new FilteredElementCollector( doc )
// .OfClass( typeof( Pipe ) )
// .ToElements()
// .Cast<Pipe>() );

//int n = pipes.Count;

int n = pipes.Count;
//// If there are less than two,
//// there is nothing we can do.

// If there are less than two,
// there is nothing we can do.
//if( 2 > n )
//{
// message = _prompt;
// return Result.Failed;
//}

if( 2 > n )
{
message = _prompt;
return Result.Failed;
}
//// If there are exactly two, pick those.

// If there are exactly two, pick those.
//if( 2 < n )
//{
// // Else, check for a pre-selection.

if( 2 < n )
{
// Else, check for a pre-selection.
// pipes.Clear();

pipes.Clear();
// Selection sel = uidoc.Selection;

Selection sel = uidoc.Selection;
// //n = sel.Elements.Size; // 2014

//n = sel.Elements.Size; // 2014
// ICollection<ElementId> ids
// = sel.GetElementIds(); // 2015

ICollection<ElementId> ids
= sel.GetElementIds(); // 2015
// n = ids.Count; // 2015

n = ids.Count; // 2015
// Debug.Print( "{0} pre-selected elements.",
// n );

Debug.Print( "{0} pre-selected elements.",
n );
// // If two or more model pipes were pre-
// // selected, use the first two encountered.

// If two or more model pipes were pre-
// selected, use the first two encountered.
// if( 1 < n )
// {
// //foreach( Element e in sel.Elements ) // 2014

if( 1 < n )
{
//foreach( Element e in sel.Elements ) // 2014
// foreach( ElementId id in ids ) // 2015
// {
// Pipe c = doc.GetElement( id ) as Pipe;

foreach( ElementId id in ids ) // 2015
{
Pipe c = doc.GetElement( id ) as Pipe;
// if( null != c )
// {
// pipes.Add( c );

if( null != c )
{
pipes.Add( c );
// if( 2 == pipes.Count )
// {
// Debug.Print( "Found two model pipes, "
// + "ignoring everything else." );

if( 2 == pipes.Count )
{
Debug.Print( "Found two model pipes, "
+ "ignoring everything else." );
// break;
// }
// }
// }
// }

break;
}
}
}
}
// // Else, prompt for an
// // interactive post-selection.

// Else, prompt for an
// interactive post-selection.
// if( 2 != pipes.Count )
// {
// pipes.Clear();

if( 2 != pipes.Count )
{
pipes.Clear();
// try
// {
// Reference r = sel.PickObject(
// ObjectType.Element,
// new PipeElementSelectionFilter(),
// "Please pick first pipe." );

try
{
Reference r = sel.PickObject(
ObjectType.Element,
new PipeElementSelectionFilter(),
"Please pick first pipe." );
// pipes.Add( doc.GetElement( r.ElementId )
// as Pipe );
// }
// catch( Autodesk.Revit.Exceptions
// .OperationCanceledException )
// {
// return Result.Cancelled;
// }

pipes.Add( doc.GetElement( r.ElementId )
as Pipe );
}
catch( Autodesk.Revit.Exceptions
.OperationCanceledException )
{
return Result.Cancelled;
}
// try
// {
// Reference r = sel.PickObject(
// ObjectType.Element,
// new PipeElementSelectionFilter(),
// "Please pick second pipe." );

try
{
Reference r = sel.PickObject(
ObjectType.Element,
new PipeElementSelectionFilter(),
"Please pick second pipe." );
// pipes.Add( doc.GetElement( r.ElementId )
// as Pipe );
// }
// catch( Autodesk.Revit.Exceptions
// .OperationCanceledException )
// {
// return Result.Cancelled;
// }
// }
//}

pipes.Add( doc.GetElement( r.ElementId )
as Pipe );
}
catch( Autodesk.Revit.Exceptions
.OperationCanceledException )
{
return Result.Cancelled;
}
}
JtPairPicker<Pipe> picker
= new JtPairPicker<Pipe>( uidoc );

Result rc = picker.Pick();

if( Result.Failed == rc )
{
message = _prompt;
}

if( Result.Succeeded != rc )
{
return rc;
}

IList<Pipe> pipes = picker.Selected;

// Extract data from the two selected pipes.

double wall_thickness = GetWallThickness( pipes[0] );
Expand Down
7 changes: 4 additions & 3 deletions BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
// 2014-10-11 2015.0.115.0 implemented CmdNewCrossFitting
// 2014-10-11 2015.0.115.1 fixed some of the warnings about deprecated use of Selection.Elements collection
// 2014-11-10 2015.0.115.2 converted CmdNewSweptBlend to use manual transaction mode and implemented CreateNewSweptBlendArc
// 2014-11-10 2015.0.116.0 implemented CmdDimensionInstanceOrigin and JtPairPicker
// 2014-11-12 2015.0.116.0 implemented CmdDimensionInstanceOrigin and JtPairPicker
// 2014-11-13 2015.0.116.1 use JtPairPicker in CmdRollingOffset
//
[assembly: AssemblyVersion( "2015.0.116.0" )]
[assembly: AssemblyFileVersion( "2015.0.116.0" )]
[assembly: AssemblyVersion( "2015.0.116.1" )]
[assembly: AssemblyFileVersion( "2015.0.116.1" )]

0 comments on commit 84f4748

Please sign in to comment.