public void PickPointsForArea(
UIDocument uidoc)
{
Document doc = uidoc.Document;
View view = doc.ActiveView;
#region COMPLICATED_UNRELIABLE_DOES_NOT_WORK
#if COMPLICATED_UNRELIABLE_DOES_NOT_WORK
using( Transaction t = new Transaction( doc ) )
{
t.Start( "Set Work Plane" );
Plane plane = new Plane(
view.ViewDirection,
view.Origin );
//SketchPlane sp = doc.Create.NewSketchPlane( plane );
SketchPlane sp = SketchPlane.Create( doc, plane );
view.SketchPlane = sp;
view.ShowActiveWorkPlane();
t.Commit();
}
double differenceX;
double differenceY;
double differenceZ;
double area;
XYZ pt1 = uidoc.Selection.PickPoint();
XYZ pt2 = uidoc.Selection.PickPoint();
double pt1x = pt1.X;
double pt1y = pt1.Y;
double pt1z = pt1.Z;
double pt2x = pt2.X;
double pt2y = pt2.Y;
double pt2z = pt2.Z;
bool b;
int caseSwitch = 0;
if( b = ( pt1z == pt2z ) )
{ caseSwitch = 1; }
if( b = ( pt1y == pt2y ) )
{ caseSwitch = 2; }
if( b = ( pt1x == pt2x ) )
{ caseSwitch = 3; }
switch( caseSwitch )
{
case 1:
differenceX = pt2x - pt1x;
differenceY = pt1y - pt2y;
area = differenceX * differenceY;
break;
case 2:
differenceX = pt2x - pt1x;
differenceZ = pt1z - pt2z;
area = differenceX * differenceZ;
break;
default:
differenceY = pt2y - pt1y;
differenceZ = pt1z - pt2z;
area = differenceY * differenceZ;
break;
}
#endif // COMPLICATED_UNRELIABLE_DOES_NOT_WORK
#endregion // COMPLICATED_UNRELIABLE_DOES_NOT_WORK
XYZ p1, p2;
try
{
p1 = uidoc.Selection.PickPoint(
"Please pick first point for area" );
p2 = uidoc.Selection.PickPoint(
"Please pick second point for area" );
}
catch( Autodesk.Revit.Exceptions.OperationCanceledException )
{
return;
}
Plane plane = view.SketchPlane.GetPlane();
UV q1 = plane.ProjectInto( p1 );
UV q2 = plane.ProjectInto( p2 );
UV d = q2 - q1;
double area = d.U * d.V;
area = Math.Round( area, 2 );
if( area < 0 )
{
area = area * ( -1 );
}
TaskDialog.Show( "Area", area.ToString() );
}