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
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
Try add PatternAngle right after this line:
MyHatch.AppendLoop(HatchLoopTypes.Outermost, IDS)
Just an idea, sorry, I can't check it now
~'J'~
This link is in C#. But anyway, check it out.
http://adndevblog.typepad.com/autocad/2012/07/hatch-using-the-autocad-net-api.html
Can't find what you're looking for? Ask the community or share your knowledge.