The code example contained within the AutoCAD NET Developers Guide does not provide
for arbitrary pattern values. It is simply a rudimentary code sample that reflects the means
of adding a Hatch pattern to a drawing. That is fine if that is what is needed. For setting the
Hatch pattern to a variable set of values the hatch pattern code can be as follows:
Dim acHatch As Hatch = New Hatch() ' 'Add the hatch to the block table record and the transaction acBlkTblRec.AppendEntity(acHatch) acTrans.AddNewlyCreatedDBObject(acHatch, True) acHatch.SetDatabaseDefaults(acSpoolDb) ' ' Change from read to write mode acHatch.UpgradeOpen() ' 'set the Hatch properties acHatch.Associative = True acHatch.PatternScale = 0.5 acHatch.PatternAngle = Math.PI * 0.25 acHatch.SetHatchPattern(HatchPatternType.PreDefine
d, "ANSI31") ' ' Change from write to read mode acHatch.DowngradeOpen() ' 'Attach to boundary loop acHatch.AppendLoop(HatchLoopTypes.[Default], acObjIdColl) ' ' Save the new hatch object to the database acTrans.Commit()
For me the CommandFlag.Session flag was set in order for the hatch to display.
If one has a better way of adding hatching that accepts differing parameters and displays correctly, then by all means share.
You sure about that?
I'm getting confused now. I was playing around with the example in the developers guide, and was just getting ready to post a comment here when you posted your last one.
I was getting the hatch showing up with the right pattern angle, but the wrong scale, regardless of calling evaluate hatch or not.
And even with the code you just posted, I am still getting an eInvalidInput Exception unless I move the PatternAngle setting to after calling SetHatchPattern.
Now it is getting even wierder, because I have gone back to the original Developer guide code, added two lines to set the PatternAngle and PatternScale, and now it is working fine. I have three separate functions each slightly different, and all three are working fine.
If I take any of the three and move the PatternAngle setting to before the SetHatchPattern call, they all fail, eInvalidInput.
>> unless I move the PatternAngle setting to after calling SetHatchPattern
Did you ever verify on what AutoCAD releases you are working? Because the order for PatternAngle before or after SetHatchPattern is depending on AutoCAD-release!
I use this code working now from 2007 to 2012, not yet tested with 2013
If ISHAcConn.AcadApp_VersionDouble < 18.1 Then Hatch.PatternAngle = (RotationDegree / 180 * Math.PI) End If
nType.PreDefined, tPatternName) If ISHAcConn.AcadApp_VersionDouble >= 18.1 Then Hatch.PatternAngle = (RotationDegree / 180 * Math.PI) End If
BTW: I'm really surprised that the hatch-creation is working without the .EvaluateHatch
- alfred -
Due to my excitement in getting this to finally work I made an error in my latest code.
UpgradeOpen and DowngradeOpen lines are not required and do nothing. I have subsequently
removed those lines and the code still works as desired.
During my attempts to remedy the problem, I had a suspicion that sequence order was important
and was moving the order around without much success in getting the correct sequence. Upon review
of my original post, I see where I have gone astray.
I am using a AutoCAD 2010 - 64bit platform with VS2010 Pro
Thank you Alfred, as I am glad to know that I am going to need to implement a caveat for later versions of Autocad.
I think this can be put to rest now
I have the same problem with the hatch scale. I've tried re-arranging the hatch properties, but I just can't seem to get it to visually show the correct hatch scale. When I pick the hatch and view its properties, it shows the right hatch scale, but visually it's wrong. Can you take a look and see where I might be going wrong?
Public Sub AddIndexArea(ByVal leftX As Double, ByVal rightX As Double, ByVal bottomY As Double, ByVal topY As Double, dwg_scale As Double) ' Get the current document and database Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database ' Start a transaction Using a1IndexAreaTrans As Transaction = acCurDb.TransactionManager.StartTransaction() ' Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = a1IndexAreaTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) ' Open the Block table record Model space for write Dim acBlkTblRec As BlockTableRecord acBlkTblRec = a1IndexAreaTrans.GetObject(acBlkTbl(BlockTableReco
rd.ModelSpace), OpenMode.ForWrite) ' Create a rectangle object for the boundary of the hatch Dim a1IndexArea As New Autodesk.AutoCAD.Geometry.Point3dCollection a1IndexArea.Add(New Geometry.Point3d(leftX, bottomY, 0)) a1IndexArea.Add(New Geometry.Point3d(rightX, bottomY, 0)) a1IndexArea.Add(New Geometry.Point3d(rightX, topY, 0)) a1IndexArea.Add(New Geometry.Point3d(leftX, topY, 0)) Dim a1IndexAreaPLine As New DatabaseServices.Polyline2d(DatabaseServices.Poly2 dType.SimplePoly, a1IndexArea, 0, True, 0, 0, Nothing) acBlkTblRec.AppendEntity(a1IndexAreaPLine) a1IndexAreaTrans.AddNewlyCreatedDBObject(a1IndexAr eaPLine, True) ' Adds the rectangle to an object id collection Dim a1IndexAreaIdColl As ObjectIdCollection = New ObjectIdCollection() a1IndexAreaIdColl.Add(a1IndexAreaPLine.ObjectId) acCurDb.TransactionManager.QueueForGraphicsFlush() ' Create the hatch object and append it to the block table record Dim acHatch As Hatch = New Hatch() acBlkTblRec.AppendEntity(acHatch) a1IndexAreaTrans.AddNewlyCreatedDBObject(acHatch, True) ' Set the properties of the hatch object ' Associative must be set after the hatch object is appended to the block table record acHatch.SetDatabaseDefaults() acHatch.Associative = True acHatch.SetHatchPattern(HatchPatternType.PreDefine d, "ANSI31") acHatch.PatternScale = dwg_scale acBlkTblRec.SetObjectIdsInFlux() acHatch.AppendLoop(HatchLoopTypes.Outermost, a1IndexAreaIdColl) acHatch.EvaluateHatch(True) ' Save the new object to the database a1IndexAreaTrans.Commit() End Using End Sub
Start with some of our most frequented solutions to get help installing your software.