Problem with Hatches in the Background in AutoCAD .NET

Problem with Hatches in the Background in AutoCAD .NET

M_Kocyigit
Enthusiast Enthusiast
165 Views
1 Reply
Message 1 of 2

Problem with Hatches in the Background in AutoCAD .NET

M_Kocyigit
Enthusiast
Enthusiast

I am facing an issue with hatch objects in AutoCAD, where the boundaries are not appearing in the foreground. Despite using the following code, the boundaries remain behind the hatch rather than in the foreground.

I have checked and correctly set all relevant system variables, such as DRAWORDERCTL, HPDRAWORDER, and SORTENTS, but the issue persists.

Can anyone help identify the cause or suggest a solution?

private void
MoveAllIncludedHatchToBack ( ObjectId objId )
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db  = doc.Database;
    Editor   ed  = doc.Editor;

    using ( doc.LockDocument() )
    {
        using ( Transaction transaction     = objId.Database.TransactionManager.StartTransaction() )
        {
            BlockReference   blockReference = transaction.GetObject ( objId, OpenMode.ForRead, true ) as BlockReference;

            BlockTableRecord btr            = transaction.GetObject ( blockReference.BlockTableRecord, OpenMode.ForRead ) as BlockTableRecord;

            ObjectIdCollection hatchIds = new ObjectIdCollection();

            bool collected = CollectHatchEntities ( transaction, blockReference.BlockTableRecord, hatchIds );

            if ( hatchIds.Count > 0 && collected )
            {
                bool moved = MoveObjectsToBack ( transaction, blockReference.BlockTableRecord, hatchIds );

                if ( moved )
                {
                    ed.WriteMessage($"\n{hatchIds.Count} hatch(es) have been moved to the back.");
                }
            }
            else
            {
                ed.WriteMessage("\nNo hatches found in the block.");
            }

            transaction.Commit();
        }
    }

}

private static bool
CollectHatchEntities ( Transaction tr, ObjectId blockTableRecordId, ObjectIdCollection hatchIds )
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor   ed  = doc.Editor;

    BlockTableRecord btr = tr.GetObject(blockTableRecordId, OpenMode.ForRead) as BlockTableRecord;

    if ( btr == null )
    {
        return false;
    }

    foreach ( ObjectId entId in btr )
    {
        Entity ent = tr.GetObject ( entId, OpenMode.ForRead ) as Entity;

        ed.WriteMessage( "\n" + ent.GetType() );

        if ( ent is Hatch )
        {
            hatchIds.Add ( entId );

            ed.WriteMessage( " --> " + "added in List" );
        }
    }

    return true;
}

private static bool
MoveObjectsToBack ( Transaction tr, ObjectId blockTableRecordId, ObjectIdCollection objectIds )
{
    BlockTableRecord btr = tr.GetObject ( blockTableRecordId, OpenMode.ForWrite ) as BlockTableRecord;

    if ( btr == null || objectIds.Count == 0 )
    {
        return false;
    }


    DrawOrderTable drawOrder = tr.GetObject ( btr.DrawOrderTableId, OpenMode.ForWrite ) as DrawOrderTable;

    if ( drawOrder == null)
    {
        return false;
    }

    drawOrder.MoveToBottom ( objectIds );

    return true;
}

 

0 Likes
Accepted solutions (1)
166 Views
1 Reply
Reply (1)
Message 2 of 2

ActivistInvestor
Mentor
Mentor
Accepted solution

Try adding the first entity in each block to the DrawOrderTable along with the hatches, before calling MoveToBottom().