Author Topic: insert block  (Read 1602 times)

0 Members and 1 Guest are viewing this topic.

samideqlqpart

  • Newt
  • Posts: 40
insert block
« on: December 29, 2020, 01:28:28 PM »
hello everybody
Autodesk.AutoCAD.Runtime.Exception : 'eNotInDatabase

this is a part of topic ; insert a symbol in a table

When i lunch my application for the first time, the block is inserting at the place i want .ok
the problem occurs when i ran my application for the second time
and i got this message

Autodesk.AutoCAD.Runtime.Exception : 'eNotInDatabase

How to fix it
thank you

the code below

   
Code: [Select]
DrawRect(acBlkTblRec, acTrans, 0, 0, 5, 5)

Public Sub DrawRect(ByRef acBlkTblRec As BlockTableRecord, ByRef actrans As Transaction,
  ByVal origPtx As Double, ByVal origPty As Double, ByVal Base As Double, ByVal Haut As Double)

        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Dim acLyrTbl As LayerTable
        acLyrTbl = actrans.GetObject(acCurDb.LayerTableId,
                                     OpenMode.ForRead)

        Dim LCadre As String = "Armat"

        Dim bt As BlockTable = DirectCast(actrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), BlockTable)
        Dim btr As BlockTableRecord = New BlockTableRecord()

        If Not bt.Has("Ceramic") Then
            bt.UpgradeOpen()

            btr.Name = "Ceramic"
            bt.Add(btr)
            actrans.AddNewlyCreatedDBObject(btr, True)
        End If

        Dim acPoly As Polyline = New Polyline()

        acPoly.AddVertexAt(0, New Point2d(origPtx + 0.15, origPty + Haut - 0.1), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx + 0.12, origPty + Haut + 0.05), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx + Base + 0.1, origPty + Haut - 0.05), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx + Base, origPty), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx, origPty), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx, origPty + Haut), 0, 0.005, 0.005)
            acPoly.AddVertexAt(0, New Point2d(origPtx + 0.1, origPty + Haut - 0.1), 0, 0.005, 0.005)


            acPoly.Layer = LCadre
        'acBlkTblRec.AppendEntity(acPoly)
        'actrans.AddNewlyCreatedDBObject(acPoly, True)
        btr.AppendEntity(acPoly)
       actrans.AddNewlyCreatedDBObject(acPoly, True)' [b][color=red]problem is here![/color][/b]
End Sub

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: insert block
« Reply #1 on: December 29, 2020, 05:16:38 PM »
Hi,
you add the polyline to the block definition outside of the the if statement, so if the block table already contains the block definition, 'btr' does not have been added to the database.
You should create the new instance of BlockTableRecord and append id a polyline only if the the block table does not already contain the block definition, IOW inside the if statement.

Code - vb.net: [Select]
  1.  DrawRect(acBlkTblRec, acTrans, 0, 0, 5, 5)
  2.  
  3. Public Sub DrawRect(ByRef acBlkTblRec As BlockTableRecord, ByRef actrans As Transaction,
  4.   ByVal origPtx As Double, ByVal origPty As Double, ByVal Base As Double, ByVal Haut As Double)
  5.  
  6.         Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  7.         Dim acCurDb As Database = acDoc.Database
  8.         Dim acLyrTbl As LayerTable
  9.         acLyrTbl = actrans.GetObject(acCurDb.LayerTableId,
  10.                                      OpenMode.ForRead)
  11.  
  12.         Dim LCadre As String = "Armat"
  13.  
  14.         Dim bt As BlockTable = DirectCast(actrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), BlockTable)
  15.  
  16.         If Not bt.Has("Ceramic") Then
  17.             bt.UpgradeOpen()
  18.             Dim btr As BlockTableRecord = New BlockTableRecord()
  19.  
  20.             btr.Name = "Ceramic"
  21.             bt.Add(btr)
  22.             actrans.AddNewlyCreatedDBObject(btr, True)
  23.  
  24.             Dim acPoly As Polyline = New Polyline()
  25.  
  26.             acPoly.AddVertexAt(0, New Point2d(origPtx + 0.15, origPty + Haut - 0.1), 0, 0.005, 0.005)
  27.             acPoly.AddVertexAt(0, New Point2d(origPtx + 0.12, origPty + Haut + 0.05), 0, 0.005, 0.005)
  28.             acPoly.AddVertexAt(0, New Point2d(origPtx + Base + 0.1, origPty + Haut - 0.05), 0, 0.005, 0.005)
  29.             acPoly.AddVertexAt(0, New Point2d(origPtx + Base, origPty), 0, 0.005, 0.005)
  30.             acPoly.AddVertexAt(0, New Point2d(origPtx, origPty), 0, 0.005, 0.005)
  31.             acPoly.AddVertexAt(0, New Point2d(origPtx, origPty + Haut), 0, 0.005, 0.005)
  32.             acPoly.AddVertexAt(0, New Point2d(origPtx + 0.1, origPty + Haut - 0.1), 0, 0.005, 0.005)
  33.  
  34.             acPoly.Layer = LCadre
  35.             btr.AppendEntity(acPoly)
  36.             actrans.AddNewlyCreatedDBObject(acPoly, True)
  37.         End If
  38. End Sub
  39.  
  40.  
Speaking English as a French Frog

samideqlqpart

  • Newt
  • Posts: 40
Re: insert block
« Reply #2 on: December 30, 2020, 02:47:28 AM »
yes it's on target
thank you Gile
this topic is resolved :smitten: