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

Hatch enotindatabase

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
DeZert
1776 Views, 5 Replies

Hatch enotindatabase

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

5 REPLIES 5
Message 2 of 6
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 6
DeZert
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 Smiley Embarassed

Message 4 of 6
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 6
mzakiralam
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....
Message 6 of 6
DeZert
in reply to: DeZert

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

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