How do I pick a ModelLine?

How do I pick a ModelLine?

stefanome
Collaborator Collaborator
719 Views
3 Replies
Message 1 of 4

How do I pick a ModelLine?

stefanome
Collaborator
Collaborator

I can use the following code to pick a Face:

var pickedObject = uidoc.Selection.PickObject(ObjectType.Face, "Select a face");
var element = doc.GetElement(pickedObject);
var face = element.GetGeometryObjectFromReference(pickedObject) as Face;

 But I can't use the following code to pick a ModelLine:

var pickedObject = uidoc.Selection.PickObject(ObjectType.Element, "Select a curve");
var element = doc.GetElement(pickedObject);
var modelLine = element.GetGeometryObjectFromReference(pickedObject) as ModelLine;

The last line reports an error: "Cannot convert type 'Autodesk.Revit.DB.GeometryObject' to 'ModelLine' via a built-in conversion".

If I change the cast on the last line to Curve, it doesn't complain, but it doesn't work.

 

I checked some of the types and this is the result:

var pickedObject = uidoc.Selection.PickObject(ObjectType.Element, "Select a curve");
var element = doc.GetElement(pickedObject);
Debug.Print(element.GetType().Name);
// output: ModelLine
var geometryObject = element.GetGeometryObjectFromReference(pickedObject);
Debug.Print(geometryObject.GetType().Name);
// output: GeometryElement
var curve = geometryObject as Curve;
// curve is null

 

0 Likes
Accepted solutions (1)
720 Views
3 Replies
Replies (3)
Message 2 of 4

franciscopossetto
Advocate
Advocate
Accepted solution

Hey,

 

I have tested this, and it worked on my side.

Reference pickObject = uiDoc.Selection.PickObject(ObjectType.Element);
ModelLine ml = doc.GetElement(pickObject.ElementId) as ModelLine;
Curve curve = ml.GeometryCurve;
XYZ endPoint = curve.GetEndPoint(1); // it return the end point correctly.

 

I hope it helps,

Kind regards.

Github:
https://github.com/franpossetto
0 Likes
Message 3 of 4

stefanome
Collaborator
Collaborator

Thanks, for some reason I thought that I was supposed to always use GetGeometryObjectFromReference.

0 Likes
Message 4 of 4

sameh.nabil
Participant
Participant

i have another way

1) select the element

2) delete the element

3) rollback

4) check the model lines in deleted elements

 

var pickedObject = uidoc.Selection.PickObject(ObjectType.Face, "Select a Floor");
var element = doc.GetElement(pickedObject);
var flor= element.GetGeometryObjectFromReference(pickedObject) as Floor;

ICollection<ElementId> ele_ids = new List<ElementId>();

Transaction transtemp = new Transaction(Cur_doc, "temp trans");
transtemp.Start();

ele_ids = Cur_doc.Delete(flor.Id);

transtemp.RollBack();

foreach (ElementId ele_id in ele_ids)
{
Element ele = Cur_doc.GetElement(ele_id);
if (ele is ModelLine)
{
LocationCurve curvyLoc = ele.Location as LocationCurve;
Curve curvy = curvyLoc.Curve;
Operation_function(ele, curvy);
}
}

 

thanks

Sameh Nabil

0 Likes