Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

DraworderTable from a Xref in c#

3 REPLIES 3
Reply
Message 1 of 4
hsiegenthaler
229 Views, 3 Replies

DraworderTable from a Xref in c#

What I want:

I have a drawing, in this drawing it has 1-n Xref.

In the xref it has 3dPoly, 2dPoly, lines and arcs.

Also in the drawing it has 3dpoly, 2dpoly, lines and arcs.

 

I have a function that returns all ObjectIds of entities that are on top of each other.

These can also be entities that are in Xref.

What I need is a function that tells me which entity is on top.

 

e.g.

Current drawing has 2 polylines on top of each other.

The drawing has 2 xrefs, each of which also has 2 polylines on top of each other.

So I have 6 entities that are on top of each other.

The function has to return the entity that is on top.

Is that even possible?

3 REPLIES 3
Message 2 of 4
cuongtk2
in reply to: hsiegenthaler

If on top entity is xref, you can open entity.BlockId as BlockTableRecord and get top entity in this. But that might not be a pline.
Message 3 of 4
hsiegenthaler
in reply to: cuongtk2

First - Thank you for you repley

Ok and how I'm doing that?

any definitions/method for doing that

 

PS: I but my code

At row 197 start the function where I need, to evaluete the top entity

 

 

Message 4 of 4
cuongtk2
in reply to: hsiegenthaler

internal class CadClass1
{
    [CommandMethod("NCv")]
    
    public static void ncv()
    {
        Document doc = AcadApp.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        var ed = doc.Editor; 
        var ptr = ed.GetPoint("\nPick point");
        if (ptr.Status == PromptStatus.OK) 
        {
            var pt = ptr.Value;
            ObjectNear objnear = new ObjectNear() { Id = ObjectId.Null, Z = -100 }; 
            using (DocumentLock lk = doc.LockDocument())
            {
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var cspace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
                    objnear = FindNearPoint(cspace, pt, objnear, tr);
                }
            }  
            ed.WriteMessage("Top curve Id: " + objnear.Id.ToString() + ", and Z:" + objnear.Z.ToString());
        }            
    }   

    /// <summary>
    /// class to save ObjectId and MaxZ
    /// </summary>
    internal class ObjectNear
    {
        public ObjectId Id { get; set; }
        public double Z { get; set; }
    }

    /// <summary>
    /// Check distance from point3d to curve on XY plane < 0.01 
    /// </summary>
    /// <param name="cv"></param>
    /// <param name="pt"></param>
    /// <returns></returns>
    private static bool IsPointNearCurve(Curve cv, Point3d pt)
    {
        var pc = cv.GetClosestPointTo(pt, false);
        return pt.Convert2d(new Plane()).GetDistanceTo(pc.Convert2d(new Plane())) < 0.01;            
    }

    /// <summary>
    /// Get Elevation of pickpoint on curve 
    /// </summary>
    /// <param name="cv"></param>
    /// <param name="pickpoint"></param>
    /// <returns></returns>
    private static double GetZpoint(Curve cv, Point3d pickpoint) => cv.GetClosestPointTo(pickpoint, false).Z;

    /// <summary>
    /// Find curve near a point from BTR currentspace
    /// </summary>
    /// <param name="btr"></param>
    /// <param name="pickpoint"></param>
    /// <param name="objnear"></param>
    /// <param name="tr"></param>
    /// <returns></returns> 
    private static ObjectNear FindNearPoint(BlockTableRecord btr, Point3d pickpoint, ObjectNear objnear, Transaction tr)
    {           
            foreach (ObjectId item in btr)
            {
                var ent = tr.GetObject(item, OpenMode.ForRead) as Entity;
                if (ent is Curve cv)
                {
                    if (IsPointNearCurve(cv, pickpoint) && objnear.Z <= GetZpoint(cv, pickpoint))
                    {
                        objnear.Id = cv.Id;
                        objnear.Z = GetZpoint(cv, pickpoint);
                    }
                }
                else if (ent is BlockReference br )
                {
                    var matrix = br.BlockTransform;
                    var newpt = pickpoint.TransformBy(matrix);
                    var btrblock = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;                       
                    objnear = FindNearPoint(btrblock, newpt, objnear, tr);
                }
            }  
        return objnear;
    }         
}

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report