Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating an associative hatch in C# causes access violation

1 REPLY 1
Reply
Message 1 of 2
Antti.Karanta
659 Views, 1 Reply

Creating an associative hatch in C# causes access violation

 

I wrote code to programmatically (in C#) create a hatch that I first created interactively, i.e. the properties of the bounding polyline, the hatch and the hatch loop are taken from that real case. The problem is that I get access violation when calling AppendLoop method.

 

Here's the code - it is easy to copy-paste into a class and run using the command NSASSOCHATCH

 

Snippet

[CommandMethod"NSASSOCHATCH" )]
public static void AssocHatch() {
    var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    using ( var tx = doc.TransactionManager.StartTransaction() ) {
        var polyline = CreatePolyline();
        AddEntity( tx, doc.Database, polyline );
        polyline.Layer = "0";
 
        CreateHatch( doc, tx, polyline );
 
        tx.Commit();
    }
}
 
private static readonly EntityColor __color = new EntityColorColorMethod.ByLayer, 256 );
 
 
private static Hatch CreateHatch( Autodesk.AutoCAD.ApplicationServices.Document doc, Transaction tx, Polyline boundary ) {
    var h = new Hatch();
 
    h.SetHatchPattern( HatchPatternType.PreDefined, "ANGLE" );
    h.HatchObjectType = HatchObjectType.HatchObject;
 
    //using ( var c = Color.FromEntityColor( __bgColor ) ) {
    //    h.BackgroundColor = c;
    //}
 
    h.SetGradient( GradientPatternType.PreDefinedGradient, "LINEAR" );
    h.GradientAngle = 0;
    h.GradientOneColorMode = false;
    h.GradientShift = 0;
 
    h.HatchStyle = HatchStyle.Outer;
 
    h.Normal = new Vector3d001 );
    h.Origin = new Point2d00 );
 
    h.PatternAngle = 0;
    h.PatternDouble = false;
    h.PatternScale = 1.0;
    h.PatternSpace = 1.0;
 
    var gradientColors = new GradientColor[] { new GradientColorColor.FromColorIndex( ColorMethod.ByAci, 5 ) , 0),
                                               new GradientColorColor.FromColorIndex( ColorMethod.ByAci, 2 ), 1)};
 
    h.SetGradientColors( gradientColors );
    // according to http://adndevblog.typepad.com/autocad/2012/07/hatch-using-the-autocad-net-api.html
    // the pattern needs to be "reaffirmed"
    h.SetHatchPattern( HatchPatternType.PreDefined, "ANGLE" );
 
    // add to db before making hatch associative
    AddEntity( tx, doc.Database, h );
 
    h.Associative = true;
 
    var boundaryColl = new ObjectIdCollectionnew [] { boundary.ObjectId } );
    // this call causes access violation
    h.AppendLoop( HatchLoopTypes.Derived | HatchLoopTypes.Polyline | HatchLoopTypes.External, boundaryColl );
 
    h.EvaluateHatch( true );
 
    return h;
}
 
private static Polyline CreatePolyline() {
    var polyline = new Polyline5 );
    polyline.Normal = new Vector3d0.00.01.0 );
    // changing this to true does not help to the access violation
    polyline.Closed = false;
    using ( var c = Color.FromEntityColor( __color ) ) {
        polyline.Color = c;
    }
 
    var startPoint = new Point2d-1689.42165011024992.835483259347 );
 
    polyline.AddVertexAt( 0, startPoint, 000 );
    polyline.AddVertexAt( 1new Point2d-551.330950840098109.713933732506 ), 000 );
    polyline.AddVertexAt( 2new Point2d29.82175404402911216.64025798278 ), 000 );
    polyline.AddVertexAt( 3new Point2d1052.892651515631313.42070640714 ), 000 );
    polyline.AddVertexAt( 4, startPoint, 000 );
 
    polyline.MinimizeMemory();
 
    return polyline;
}
 
private static ObjectId AddEntity( Transaction tx, Database db, Entity e ) {
    var btr = (BlockTableRecord)tx.GetObject( SymbolUtilityServices.GetBlockModelSpaceId( db ), OpenMode.ForWrite );
    var oid = btr.AppendEntity( e );
    e.SetDatabaseDefaults( db );
    tx.AddNewlyCreatedDBObject( e, true );
    return oid;
}

 

 

Environment: windows 7 on x64 (also occurs at least on windows 8), AutoCAD 2015

 

1 REPLY 1
Message 2 of 2
kdub_nz
in reply to: Antti.Karanta

 

http://www.theswamp.org/index.php?topic=50188.msg553642#msg553642


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

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

Post to forums  

”Boost