Hi Guys,
I need to add a hatch fill polyline area but leave the text island out.
What I did is put put Polyline ID and Text island ID into an ObjectIDCollection. The code doesn't throw any error but I can't see the hatch.
Public Function AutoCADAddHatch(ByRef newAddedHatch As Hatch, ByRef HatchElementIDs As ObjectIdCollection, ByVal HatchColour As Autodesk.AutoCAD.Colors.Color, _ ByVal HatchLayerName As String, Optional ByVal PatternName As String = "SOLID", Optional ByRef strError As String = "") As Boolean Dim db As Database = acApp.DocumentManager.MdiActiveDocument.Database Try Using objTrans As Transaction = db.TransactionManager.StartTransaction Dim objBT As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite) Dim objNewBTR As New BlockTableRecord objNewBTR.Name = "ZoneHatch" objBT.Add(objNewBTR) newAddedHatch = New Hatch objNewBTR.AppendEntity(newAddedHatch) objTrans.AddNewlyCreatedDBObject(newAddedHatch, True) newAddedHatch.SetDatabaseDefaults() newAddedHatch.SetHatchPattern(HatchPatternType.PreDefined, PatternName) Dim objTextIDs As New ObjectIdCollection objTextIDs.Add(HatchElementIDs(1)) newAddedHatch.AppendLoop(HatchLoopTypes.TextIsland, objTextIDs) Dim objPolyIDs As New ObjectIdCollection objPolyIDs.Add(HatchElementIDs(0)) newAddedHatch.AppendLoop(HatchLoopTypes.Outermost, objPolyIDs) newAddedHatch.Color = HatchColour newAddedHatch.Layer = HatchLayerName newAddedHatch.Associative = True newAddedHatch.EvaluateHatch(True) objTrans.Commit() End Using Catch sysex As SystemException strError = sysex.Message MError.StorePlanLogError(sysex, sysex.Message, _ CallingProcedure:=STR_MODULE_NAME & "." & "AutoCADAddHatch") Catch ex As Exception strError = ex.Message MError.StorePlanLogError(ex, ex.Message, _ CallingProcedure:=STR_MODULE_NAME & "." & "AutoCADAddHatch") End Try End Function
Any ideas?
Thanks very much
Hi,
not sure if that is a help, but I always did create the hatch up to .Evaluate and afterwards I appended it to modelspace (or whatever BlockTableRecord). You did first the append to the Block and then added the borders and set the options.
And just to make sure:
a) your Polyline had the property ".Closed" set to TRUE?
b) the blockdefinition you created was inserted anywhere? I mean you just defined the BlockTableRecord "ZoneHatch" but within this code-snippet you did not insert the block in modelspace or paperspace.
- alfred -
I've just played with my AddHatch function, because I remembered there was a trick to it when I wrote mine, and It has to do with the Associative Property. If you are setting Associative = True (as my code and the OP's do), then you must add the hatch to the database before setting Associative, or you get an eNotInDatabase Exception. Also, if you move the .Associative = True to after Trans.AddNewlyCreated...., but leave the AppendLoop call before adding the Hatch to the Database, you will get a Hatch, but it will not be Associative. So, if you want an Associative Hatch, you must Add the Hatch to the Database, then set Associative to True, then AppendLoop.
(I also discovered that you must call SetHatchPattern before setting the PatternAngle property.)
The OP's code is a little different from mine, but covers all those points in the correct order, so I'm pretty sure it has to do with item b) in your post. He probably has a properly created Hatch, but didn't insert into the drawing anywhere.
Hi
I have tried both your method
Change the postion of adding object to database.
The polyline is closed as well.
Really don't know what to do now
Thanks
Hi,
just to make sure: do you have a block called "ZoneHatch" in your drawing after running your code?
Did you try to start the code in a new drawing, where the blockdefinition "ZoneHatch" did not exist before starting your code?
- alfred -
Hi
Thanks for your reply.
"ZoneHatch" is a new block name. no where else use that name. It is just for my testing. The code segment is not part of a whole application. it is just for testing perpose.
Hi,
one more idea: you create a hatch in a blockdefinition, you set the hatch associative to a polyline, but I don't see that your polyline is part of the blockdefinition. At least the property "associative" will not work that way.
- alfred -
Hi,
>> "ZoneHatch" is a new block name. no where else use that name
...and was it in the drawing after your code was executed?
- alfred -
No. The block name wasn't in the drawing after code excuted.
I am totally confused now.
I have handle for polyline and mtext. it looks like this.
I would like to make it looks like this
What should I do?
Thanks again
Hi,
>> I have handle for polyline and mtext. it looks like this.
And these items are in the modelspace?
If so - and the hatch should be associative - you have to add the hatch to the modelspace and not to a new blockdefinition.
- alfred -
Those object are in modelspace already.
I don't know how to add hatch to modelspace.
I would like to be able to swith hatch on and off really.
Thanks
Hi,
look to this sample i wrote now for you (attached TXT-file).
It defines a command, let you select a polyline and then a text, if all was ok you will get the hatch created (in the same space where the poly is placed).
IMPORTANT: I have not checked so much, I also did not take care of differences of setting the properties (between 2010 and 2011), I just created it now with 2010 and tried it once ... that said: please save your drawing before my code starts 😉
HTH, - alfred -
Thanks so much. Modelspace is the key problem. I have modified my code and got the result I need.
Public Function AutoCADAddHatch(ByRef newAddedHatch As Hatch, ByRef HatchElementIDs As ObjectIdCollection, ByVal HatchColour As Autodesk.AutoCAD.Colors.Color, _ ByVal HatchLayerName As String, Optional ByVal PatternName As String = "SOLID", Optional ByRef strError As String = "") As Boolean Dim db As Database = acApp.DocumentManager.MdiActiveDocument.Database Try Using objTrans As Transaction = db.TransactionManager.StartTransaction Dim objBT As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite) Dim objNewBTR As BlockTableRecord = objTrans.GetObject(objBT(BlockTableRecord.ModelSpace), OpenMode.ForWrite) Dim objEnt As Entity = objTrans.GetObject(HatchElementIDs(0), OpenMode.ForWrite) Dim objPoly As Polyline = DirectCast(objEnt, Polyline) Dim objNewPolyHatch As Polyline = objPoly.Clone ' objNewBTR.Name = "ZoneHatch" newAddedHatch = New Hatch newAddedHatch.SetDatabaseDefaults() newAddedHatch.SetHatchPattern(HatchPatternType.PreDefined, PatternName) Dim objTextIDs As New ObjectIdCollection objTextIDs.Add(HatchElementIDs(1)) newAddedHatch.AppendLoop(HatchLoopTypes.Textbox, objTextIDs) Dim objPolyIDs As New ObjectIdCollection objPolyIDs.Add(HatchElementIDs(0)) newAddedHatch.AppendLoop(HatchLoopTypes.Default, objPolyIDs) newAddedHatch.Color = HatchColour newAddedHatch.Layer = HatchLayerName newAddedHatch.EvaluateHatch(True) objNewBTR.AppendEntity(newAddedHatch) objTrans.AddNewlyCreatedDBObject(newAddedHatch, True) objTrans.Commit() End Using Catch sysex As SystemException strError = sysex.Message MError.StorePlanLogError(sysex, sysex.Message, _ CallingProcedure:=STR_MODULE_NAME & "." & "AutoCADAddHatch") Catch ex As Exception strError = ex.Message MError.StorePlanLogError(ex, ex.Message, _ CallingProcedure:=STR_MODULE_NAME & "." & "AutoCADAddHatch") End Try End Function
This works for me. However I still didn't understand what associative means
Thanks again
Regards,
Yaqi
Hi,
>> I still didn't understand what associative means
This is an option for making objects depending from other ones.
In case of hatch: if you modify the border the hatch recognizes this and recreates itself automatically according to the new border-position/-size.
- alfred -
Even if you use ByVal with a reference object it will still use Byref anyways which you use.
I am wondering if you have had problems with the function where you are passing in a reference object to a function that adds it to a transaction, and if you try accessing it once the function has finished?