.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Associative Hatch Error

10 REPLIES 10
Reply
Message 1 of 11
wesbird
1712 Views, 10 Replies

Associative Hatch Error

Hi,
I am writing a function to create Hatch. Everything works just fine except Associative. if I put the
oHatch.Associative = true;
get a error message:
eNotInDatabase
It will work if I remove this line.
does anybody know how to fix it? thank you very much.

Here is the code:
public static Hatch CreateHatch(Entity pline, BlockTableRecord btr, short HatchColor,
string HatchLayer, string HatchPattern, double HatchScale, double HatchAngle, bool HatchAssociate)
{
if (pline == null) return null;

Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);

bool IsSolid = (HatchPattern == "SOLID");

LayerLibrary.IsLayerLocked(HatchLayer);

if (HatchPattern.Substring(0,3).Equals("ISO")) {HatchPattern = "ACAD_" + HatchPattern;}

ObjectId oId;
if (HatchAssociate)
{
Entity pline2 = (Entity)trans.GetObject(pline.ObjectId,OpenMode.ForWrite);
oId = pline2.ObjectId;
}
else { oId = pline.ObjectId;}

ObjectIdCollection ObjIds = new ObjectIdCollection();
ObjIds.Add(oId);

Hatch oHatch = new Hatch();
try
{
oHatch.SetHatchPattern(HatchPatternType.PreDefined,HatchPattern);
oHatch.AppendLoop(0, ObjIds);

oHatch.Layer = HatchLayer;
oHatch.ColorIndex = HatchColor;
oHatch.Associative = HatchAssociate;

if (!IsSolid)
{
oHatch.PatternAngle = HatchAngle;
oHatch.PatternScale = HatchScale;
}

oHatch.EvaluateHatch(true);
btr.AppendEntity(oHatch);
trans.AddNewlyCreatedDBObject(oHatch,true);
trans.Commit();
}
catch (System.Exception caught)
{
// ddebug
ed.WriteMessage(caught.Message.ToString());
oHatch = null;
}
finally
{
trans.Dispose();
}
return oHatch;
}




Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
10 REPLIES 10
Message 2 of 11
ChrisArps
in reply to: wesbird

The error message is telling you exactly what to do.

The hatch must be added to the database first so the objectId of the hatch can be used to establish the relation between it and the boundary entities.

Try calling AddNewlyCreated first then set the associative flag to true.

You also should call Trans.Abort() in your catch block and also call oHatch.Dispose when you are done with the hatch object.

Chris Arps
Message 3 of 11
wesbird
in reply to: wesbird

Thank you, Chris.
I move
oHatch.Associative = HatchAssociate;
right after
trans.AddNewlyCreatedDBObject(oHatch,true);
it do create Hatch without error message but the hatch is non-associated.

so I add
oHatch.EvaluateHatch(true);
after Associative
still not associated.

I go back and check AutoCAD Managed Class Reference, under AcDbHatch Class, there are sample code in C++, which did not required Associate after add to database.

here is the code:
Acad::ErrorStatus acqHatch1()
{
AcDbHatch* pHatch = new AcDbHatch();

// Set hatch plane
//
AcGeVector3d normal(0.0, 0.0, 1.0);
pHatch->setNormal(normal);
pHatch->setElevation(0.0);

// Set non associative hatch
//
pHatch->setAssociative(Adesk::kFalse);

// Set hatch pattern to SolidFill type
//
pHatch->setPattern(AcDbHatch::kPreDefined, "SOLID");

// Set hatch style to kNormal
//
pHatch->setHatchStyle(AcDbHatch::kNormal);

...

// Append an external loop (rectangle) to hatch boundary

pHatch->appendLoop(AcDbHatch::kExternal, vertexPts, vertexBulges);

...
// Append an internal circular loop to hatch boundary
//
AcGeIntArray edgeTypes;
AcGeVoidPointerArray edgePtrs;
edgeTypes.append(AcDbHatch::kCirArc);
edgePtrs.append((void*)cirArc);
pHatch->appendLoop(AcDbHatch::kDefault, edgePtrs, edgeTypes);

// Elaborate solid fill
//
pHatch->evaluateHatch();

...

Any idea? Thank you very much.
Wes


Update:
In same help file, Property Associative is wrapped from AcDbHatch::associative and AcDbHatch::setAssociative,
in help item for AcDbHatch::setAssociative
"This function sets the associativity flag to true or false for the hatch entity. To create an associative hatch entity, you must use this function and set the associativity flag to true before you begin to build the boundary loops using the appendLoop(loopType, dbObjIds) or insertLoopAt(loopIndex, loopType, dbObjIds) functions. "
so I move
oHatch.Associative = HatchAssociate;
before
oHatch.AppendLoop(0, ObjIds);
error message:
eNotInDatabase
Message was edited by: weslleywang Message was edited by: weslleywang
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 4 of 11
ChrisArps
in reply to: wesbird

Since you are adding loops by objectid, then the hatch must be added to the database before calling appendLoop.

If you set the associative property to true, then the hatch tries to setup a reactor/relationship to the polyline.

Try AddObject, set associative to true, then Addloops.

Chris Arps
Message 5 of 11
wesbird
in reply to: wesbird

Not working. error message: eAmbiguousOutput
Here is the code:
Hatch oHatch = new Hatch();
try
{
oHatch.SetHatchPattern(HatchPatternType.PreDefined,HatchPattern);

oHatch.Layer = HatchLayer;
oHatch.ColorIndex = HatchColor;

if (!IsSolid)
{
oHatch.PatternAngle = HatchAngle;
oHatch.PatternScale = HatchScale;
}

oHatch.EvaluateHatch(true);
btr.AppendEntity(oHatch);
trans.AddNewlyCreatedDBObject(oHatch,true);

// todo
oHatch.Associative = HatchAssociate;
oHatch.AppendLoop(0, ObjIds);

oHatch.EvaluateHatch(true);

trans.Commit();
oHatch.Dispose();
}
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 6 of 11
ChrisArps
in reply to: wesbird

There is a sample of adding a hatch in ObjectArx sdk, but it works differently in dotnet.

Here is VB code that works:

Public Sub TestHatch()
' method was gleaned from ObjectArx2006 samples, file EntMakeTest.cpp
'
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim selOpts As New PromptSelectionOptions()
Dim res As PromptSelectionResult

selOpts.SingleOnly = True
selOpts.MessageForAdding = "Select a closed polyline"

res = ed.GetSelection(selOpts)
If res.Status <> PromptStatus.OK Then Return
Dim selId As ObjectId = res.Value.GetObjectIds(0)
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim trans As Transaction = tm.StartTransaction
Try
Dim objIds As New ObjectIdCollection()
objIds.Add(selId)

Dim hat As New Hatch()
With hat
.SetDatabaseDefaults()
.Layer = "0"
.PatternAngle = 0.0
.PatternScale = 100.0 ' I use ADT Imperial, so this works mo betta
' DEFECT: must set pattern last or angle and scale do not work
.SetHatchPattern(HatchPatternType.PreDefined, "ANGLE")
End With
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead, False)
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False)

btr.AppendEntity(hat)
trans.AddNewlyCreatedDBObject(hat, True)
'enum HatchLoopType

'kDefault 0 The loop type hasn't been specified yet.
' It turns to a "real" value as soon as some real loops get created.
'kExternal 1 A loop that consists of external entities (derived from AcDbEntity and owned by
' an AcDbDatabase). The hatch might be associative to these entities.
'kPolyline 2 The hatch loop consists of a polyline (AcGe geometry, not AcDb geometry).
'kDerived 4 A loop that was derived by AutoCAD's boundary tracer from a picked point.
'kTextbox 8 A loop that consists of a box around an existing text object (AcDbText).
'kOutermost 0x10 The outermost loop of this hatch.
'kNotClosed 0x20 A loop that is not closed.
'kSelfIntersecting 0x40 A loop that intersects with itself.
'kTextIsland 0x80 Text loops that are surrounded by even number of outer loops.
' Text island loops are avoided when performing solid fill.

hat.Associative = True
hat.AppendLoop(1, objIds)
hat.EvaluateHatch(False)
hat.Dispose()
trans.Commit()
Catch ex As Exception
MsgBox(ex.Message)
trans.Abort()
Finally
trans.Dispose()
tm.Dispose()
End Try
End Sub
Message 7 of 11
wesbird
in reply to: wesbird

Thank you, this time it works.



Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 8 of 11
ghethco
in reply to: wesbird

I'm trying to add a hatch to a block I'm creating.  Whenever I try to set the hatch's "PatternAngle", I get a 'eInvalidInput' exception.  I'm just trying to set it to 45 degrees, which works fine creating a hatch manually in the GUI.

 

Here's the code:

 

Try
    Dim myDWG As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim myDB As Database = myDWG.Database
    Dim myTrans As Transaction = myDB.TransactionManager.StartTransaction
    Dim BlockName As String = "MyBlock"
    Dim BasePoint As New Point3d(0, 0, 0)
    Dim docloc As DocumentLock = myDWG.LockDocument
    Using (docloc)
    Using (myTrans)
        Layers.SetLayer("MODULES-HATCH")
        Dim MyBT As BlockTable = CType(myTrans.GetObject(myDB.BlockTableId, OpenMode.ForWrite), BlockTable)
        For Each ObjID As ObjectId In MyBT
        Dim MyBTR As BlockTableRecord = CType(myTrans.GetObject(ObjID, OpenMode.ForWrite), BlockTableRecord)
        If (MyBTR.Name = BlockName) Then
            Dim MyPL2 As New Polyline       ' bounding rectangle
            MyPL2.AddVertexAt(0, New Point2d(LowerLeft.X + 1, LowerLeft.Y + 1), 0, 0, 0)
            MyPL2.AddVertexAt(1, New Point2d(UpperRight.X - 1, LowerLeft.Y + 1), 0, 0, 0)
            MyPL2.AddVertexAt(2, New Point2d(UpperRight.X - 1, UpperRight.Y - 1), 0, 0, 0)
            MyPL2.AddVertexAt(3, New Point2d(LowerLeft.X + 1, UpperRight.Y - 1), 0, 0, 0)
            MyPL2.AddVertexAt(4, New Point2d(LowerLeft.X + 1, LowerLeft.Y + 1), 0, 0, 0)
            MyBTR.AppendEntity(MyPL2)
            myTrans.AddNewlyCreatedDBObject(MyPL2, True)
            Dim IDS As ObjectIdCollection = New ObjectIdCollection()
            IDS.Add(MyPL2.ObjectId)

            Dim MyHatch As Hatch = New Hatch()
            MyBTR.AppendEntity(MyHatch)
            myTrans.AddNewlyCreatedDBObject(MyHatch, True)

            MyHatch.SetDatabaseDefaults()
            'MyHatch.PatternAngle = 45.0  <--- causes "eInvalidInput" exception
            MyHatch.PatternScale = 64.0
            MyHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI37")
            MyHatch.Associative = True
            MyHatch.AppendLoop(HatchLoopTypes.Outermost, IDS)
            MyHatch.EvaluateHatch(False)
            MyHatch.Dispose()
        End If
        'EntLib.Debug("Block Name = " & MyBTR.Name)
        Next
        myTrans.Commit()
    End Using
    End Using
Catch ex As System.Exception
    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message)
End Try

Message 9 of 11
ghethco
in reply to: ghethco

BTW, the angle itself doesn't matter, even with "0.0" as the angle, still get the exception.  Also tried 45 degrees converted to radians (Math.PI / 180.0) * 45.0.  Same result.

 

Gary

Message 10 of 11
Hallex
in reply to: wesbird

Try add PatternAngle right after this line:

 

MyHatch.AppendLoop(HatchLoopTypes.Outermost, IDS)

 

Just an idea, sorry, I can't check it now

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 11 of 11
Mep09Developer
in reply to: wesbird

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost