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

Add Hatch No Error but Desn't Show

14 REPLIES 14
Reply
Message 1 of 15
yaqiz
710 Views, 14 Replies

Add Hatch No Error but Desn't Show

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

14 REPLIES 14
Message 2 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 15

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.

Dave O.                                                                  Sig-Logos32.png
Message 4 of 15
yaqiz
in reply to: chiefbraincloud

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

Message 5 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 15
yaqiz
in reply to: Alfred.NESWADBA

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.

 

 

Message 7 of 15

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 9 of 15
yaqiz
in reply to: Alfred.NESWADBA

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. 

BeforeHatch.png

 

I would like to make it looks like this

Hatch.png

What should I do?

 

Thanks again

Message 10 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 11 of 15
yaqiz
in reply to: Alfred.NESWADBA

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

Message 12 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 13 of 15
yaqiz
in reply to: Alfred.NESWADBA

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

Message 14 of 15
Alfred.NESWADBA
in reply to: yaqiz

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 15 of 15
jeff
in reply to: Alfred.NESWADBA

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?

You can also find your answers @ TheSwamp

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