Create Hatch Objects using Trace Boundaries using .NET

by Fenton Webb

After sweating over some Hatch creation code, I thought I better share with you guys how I solved it.

Here’s what I wanted Hatched:

image

The way I made it work was, to first get all of the boundary geometry as Polylines using the Editor.TraceBoundary function.

This gave me an array of 3 Polylines, shown in red e.g.

image

Next, I add the polylines to the Database (temporarily) so that the Hatch object can process them (outside of any Transaction) – sorry VB Smile

dim loopObjectIds = new ObjectIdCollection()    Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database    ' add the loop objects to the dwg    For Each polyline As Polyline In loopObjects      Using curSpace As BlockTableRecord = db.CurrentSpaceId.Open(OpenMode.ForWrite)        loopObjectIds.Add(curSpace.AppendEntity(polyline))        ' make sure to close the polyline        polyline.Close()      End Using    Next

Now append each polyline obtained by the trace as a separate loop e.g.

For Each objectId In loopObjectIds

  Dim ids = New ObjectIdCollection()

  ids.Add(objectId)

  acHatch.AppendLoop(HatchLoopTypes.Outermost, ids)

Next

acHatch.EvaluateHatch(True)

That’s it!!

Also, here are the different results obtained by setting the Hatch.HatchStyle to:

HatchStyle.Ignore results in this:

image

HatchStyle.Outer results in this:

image

HatchStyle.Normal

results in this:

image

        <p>For the record, I could have used the appendLoop function which takes a Curve2dCollection, however I didn’t on this occasion because I didn’t know that we have equivalent .NET functions to these C++ versions…</p>  <p><strong>acdbConvertAcDbCurveToGelibCurve     <br />acdbConvertGelibCurveToAcDbCurve      <br />acdbAssignGelibCurveToAcDbCurve</strong></p>  <p>in .NET</p>  <p><strong>CreateFromGeCurve</strong></p>  <p><strong>SetFromGeCurve</strong>

Comments

4 responses to “Create Hatch Objects using Trace Boundaries using .NET”

  1. very useful post. Thanks for sharing

  2. Hi,
    I ‘ve been trying to implement your code in c# to get the result you have with HatchStyle.Outer and i can’t get it to work. Can you please tell me when do you write the line acHatch.HatchStyle = HatchStyle.Outer ?

  3. Imad Almuzain Avatar
    Imad Almuzain

    where is Editor.TraceBoundary in code?

  4. Well, that’s very interesting. Thanks for sharing, bro

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading