.NET

Reply
Member
Posts: 5
Registered: ‎11-24-2013
Message 1 of 6 (203 Views)
Accepted Solution

Hatch enotindatabase

203 Views, 5 Replies
11-24-2013 09:45 PM

Hi! I'm writing a small function to create block from polyline and hatch, and there was a problem with Hatch - error message ENotInDatabase on the code string "acTrans.AddNewlyCreatedDBObject(hHatch, True)"

If comment out the hatch section everything else works. So what am I doing wrong with hatch creating?

 

Sorry for my wrong english, it's not my native language.

 

 

Imports System
Imports System.IO
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry

Public Class AddPiket
    <CommandMethod("AddPiket")> _
    Public Sub AddPiket()
        Dim acDB As Database = Application.DocumentManager.MdiActiveDocument.Database

        Dim acCurEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

        Using acTrans As Transaction = acDB.TransactionManager.StartTransaction()

            Dim acBlockTbl As BlockTable = acTrans.GetObject(acDB.BlockTableId, OpenMode.ForRead)

            Dim acTypedValArr(0) As TypedValue
            acTypedValArr.SetValue(New TypedValue(DxfCode.Start, "LWPOLYLINE"), 0)

            Dim acSelFilter As SelectionFilter = New SelectionFilter(acTypedValArr)

            Dim acPrSelRest As PromptSelectionResult = acCurEd.GetSelection(acSelFilter)
            Dim acSelSet As SelectionSet = acPrSelRest.Value
  
            For Each acSelObj As SelectedObject In acSelSet
                If Not IsDBNull(acSelObj) Then
                    Dim acLwPLine As Polyline = TryCast(acTrans.GetObject(acSelObj.ObjectId, OpenMode.ForRead), Polyline)
                    Dim UseL As Double = 0
                    Dim acPt As Point3d
                    Dim prefix As String
                    Dim btrRecordHatch As BlockTableRecord = New BlockTableRecord
                    Dim acBtrModelSpace As BlockTableRecord
                    Dim acRefBl As BlockReference

                    acPt = acLwPLine.GetPointAtDist(UseL)
              
                    Dim acPoly As Polyline = New Polyline()
                    acPoly.AddVertexAt(0, New Point2d(0, 0), 0, 0, 0)
                    acPoly.AddVertexAt(1, New Point2d(2, 0), 0, 0, 0)
                    acPoly.AddVertexAt(2, New Point2d(2, 15), 0, 0, 0)
                    acPoly.AddVertexAt(3, New Point2d(0, 15), 0, 0, 0)
                    acPoly.AddVertexAt(4, New Point2d(0, 0), 0, 0, 0)
                    acPoly.Closed = True
                  
                    Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
                    acObjIdColl.Add(acPoly.ObjectId)

                    Dim hHatch As Hatch = New Hatch()
                    hHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID")
                    Dim normal As Vector3d = New Vector3d(0, 0, 1)
                    hHatch.Normal = normal
                    hHatch.Elevation = 0
                    hHatch.PatternScale = 1
                    hHatch.PatternAngle = 0

                    btrRecordHatch.AppendEntity(hHatch)
                    acTrans.AddNewlyCreatedDBObject(hHatch, True)
                    hHatch.Associative = True
                    hHatch.AppendLoop(HatchLoopTypes.Default, acObjIdColl)
                    hHatch.EvaluateHatch(True)

                     Dim acBlockTblRec As BlockTableRecord = New BlockTableRecord()
                    acBlockTblRec.Name = "piket"
                    acBlockTbl.UpgradeOpen()

                    Dim acBTRId As ObjectId = acBlockTbl.Add(acBlockTblRec)
                    acTrans.AddNewlyCreatedDBObject(acBlockTblRec, True)

                    acBlockTblRec.AppendEntity(acPoly)
                    acTrans.AddNewlyCreatedDBObject(acPoly, True)

                    acBlockTblRec.AppendEntity(hHatch)
                    acTrans.AddNewlyCreatedDBObject(hHatch, True)

                    While UseL <= acLwPLine.Length

                        acPt = acLwPLine.GetPointAtDist(UseL)

                        If Fix(UseL / 100) < 10 Then
                            prefix = "0" & Fix(UseL / 100).ToString
                        Else
                            prefix = Fix(UseL / 100).ToString
                        End If

                        acBtrModelSpace = acTrans.GetObject(acBlockTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                        If Fix(UseL / 100) Mod 2 = 0 Then
                            Dim acPiketTxt As DBText = New DBText()
                            acPiketTxt.SetDatabaseDefaults()
                            acPiketTxt.Position = New Point3d(acPt.X + 5, acPt.Y + 5, 0)
                            acPiketTxt.TextString = "ПК" & prefix & "+00"
                            acPiketTxt.Height = 5

                            acBtrModelSpace.AppendEntity(acPiketTxt)
                            acTrans.AddNewlyCreatedDBObject(acPiketTxt, True)

                        End If

                        acRefBl = New BlockReference(acPt, acBTRId)
                        Dim vector As Vector3d = acLwPLine.GetFirstDerivative(acPt)
                        Dim refVector As Vector3d = New Vector3d(1, 0, 0)
                        acRefBl.Rotation = refVector.GetAngleTo(vector)

                        acBtrModelSpace.AppendEntity(acRefBl)
                        acTrans.AddNewlyCreatedDBObject(acRefBl, True)

                        UseL = UseL + 100
                    End While
                End If
            Next
            acTrans.Commit()
        End Using
    End Sub
End Class

 Thanks

*Expert Elite*
Alfred.NESWADBA
Posts: 8,516
Registered: ‎06-29-2007
Message 2 of 6 (188 Views)

Re: Hatch enotindatabase

11-24-2013 11:47 PM in reply to: DeZert

Hi,

 

not tried, but I guess that within this line:

    acObjIdColl.Add(acPoly.ObjectId)

the objectid of acPoly is not valid, so to use it as a border for the hatch will not work.

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Member
Posts: 5
Registered: ‎11-24-2013
Message 3 of 6 (177 Views)

Re: Hatch enotindatabase

11-25-2013 02:46 AM in reply to: Alfred.NESWADBA

alfred.neswadba wrote:

the objectid of acPoly is not valid, so to use it as a border for the hatch will not work.



If not more difficult, explain how to use polyline as boundary for hatch?

I used "AutoCAD .NET Developer's Guide: Define the Hatch Boundaries" as tutorial... What different between using a circle (in sample from AutoCAD .NET Developer's Guide) and polyline as boundary?

May be my questions too elementary, but I am beginner in programming :smileyembarrassed:

*Expert Elite*
Alfred.NESWADBA
Posts: 8,516
Registered: ‎06-29-2007
Message 4 of 6 (172 Views)

Re: Hatch enotindatabase

11-25-2013 02:54 AM in reply to: DeZert

Hi,

 

>> What different between using a circle (in sample from AutoCAD .NET Developer's Guide) and polyline as boundary?

That's not the major difference between your code and the one >>>here in the developer guide<<<.

 

The major difference is that the boundary is added to the modelspace before beeing used as boundary; that 2 lines show this:

      acBlkTblRec.AppendEntity(acCirc)
      acTrans.AddNewlyCreatedDBObject(acCirc, True)

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
mzakiralam
Posts: 221
Registered: ‎11-09-2012
Message 5 of 6 (171 Views)

Re: Hatch enotindatabase

11-25-2013 02:57 AM in reply to: DeZert
Hi DeZert,

Below links could be helpful for you. Here you can find how to get boundary using api (first link) and 2nd link demostrate how to create hatch using trace boundary

http://adndevblog.typepad.com/autocad/2012/05/identifying-the-boundary-through-api-.html

http://adndevblog.typepad.com/autocad/2013/07/create-hatch-objects-using-trace-boundaries-using-net....
Member
Posts: 5
Registered: ‎11-24-2013
Message 6 of 6 (109 Views)

Re: Hatch enotindatabase

12-04-2013 08:40 PM in reply to: DeZert

Very thanks! Now I found where I made a mistake!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community