Author Topic: insert blocks  (Read 4040 times)

0 Members and 1 Guest are viewing this topic.

Humbertogo

  • Guest
insert blocks
« on: January 18, 2008, 01:47:36 PM »
How to insert blocks using vb.net 2005
and what is the best way to preview dwg

Humbertogo

  • Guest
Re: insert blocks fatal error
« Reply #1 on: January 18, 2008, 04:52:38 PM »
afther close acad get fatal error i using this function to insertblocks
what is wrong with this  function?

Code: [Select]
    Public Function InsertApiBlock()
        Dim insertionPoint(2) As Double
        Dim Comp As String
        Comp = "d:\temp\test.dwg"
        insertionPoint(0) = 10 : insertionPoint(1) = 20 : insertionPoint(2) = 0
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim tr As Transaction = doc.TransactionManager.StartTransaction


        Try
            Dim dwgName As String = HostApplicationServices.Current.FindFile( _
                                    Comp, Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
            Dim db As Database = New Database(False, False)
            db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")
            Dim BlkId As ObjectId
            BlkId = doc.Database.Insert(dwgName, db, False)

            MessageBox.Show(Comp)
            Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)
            Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
            Dim bref As BlockReference = New BlockReference(New Autodesk.AutoCAD.Geometry.Point3d(insertionPoint), BlkId)
            btr.AppendEntity(bref)
            tr.AddNewlyCreatedDBObject(bref, True)
            bref.ExplodeToOwnerSpace()
            bref.Erase()
            tr.Commit()

        Catch ex As System.Exception
            ed.WriteMessage(ex.ToString)

        End Try

    End Function

Fatty

  • Guest
Re: insert blocks
« Reply #2 on: January 18, 2008, 05:25:04 PM »
As I see in ObjectARX SDK docs this method should work
just with uniform scaled blocks
Thus, you don't defined scale factors, it would not work for you

Try this instead:
Code: [Select]
#Region " AcadDlls"
Imports System
Imports System.IO
Imports System.Collections
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports graph = Autodesk.AutoCAD.GraphicsInterface
Imports Autodesk.AutoCAD.ApplicationServices
Imports AcDb = Autodesk.AutoCAD.DatabaseServices
Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports AcGe = Autodesk.AutoCAD.Geometry
Imports AcRx = Autodesk.AutoCAD.Runtime
#End Region
Module Module5
    Sub main()
        InsertApiBlock()
    End Sub
    Public Sub InsertApiBlock()
        Dim insertionPoint(2) As Double
        Dim Comp As String

        Comp = "d:\temp\test.dwg"
        insertionPoint(0) = 10 : insertionPoint(1) = 20 : insertionPoint(2) = 0
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim tr As Transaction = doc.TransactionManager.StartTransaction


        Try
            Dim dwgName As String = HostApplicationServices.Current.FindFile( _
                                    Comp, Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)
            Dim db As Database = New Database(False, False)
            db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")
            Dim BlkId As ObjectId
            BlkId = doc.Database.Insert(dwgName, db, False)

            MessageBox.Show(Comp)
            Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)
            Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
            Dim bref As BlockReference = New BlockReference(New Autodesk.AutoCAD.Geometry.Point3d(insertionPoint), BlkId)
            Dim scl As Scale3d = New Scale3d(1, 1, 1)
            bref.ScaleFactors = scl
            btr.AppendEntity(bref)
            tr.AddNewlyCreatedDBObject(bref, True)
            bref.UpgradeOpen()
            bref.ExplodeToOwnerSpace()
            bref.Erase()
            tr.Commit()

        Catch ex As System.Exception
            ed.WriteMessage(ex.ToString)

        End Try

    End Sub
End Module

Humbertogo

  • Guest
Re: insert blocks
« Reply #3 on: January 18, 2008, 06:27:34 PM »
still have fatal error

MickD

  • King Gator
  • Posts: 3643
  • (x-in)->[process]->(y-out) ... simples!
Re: insert blocks
« Reply #4 on: January 18, 2008, 07:10:34 PM »
Can you debug to find where it's failing?

Even some temp prompts/user interaction placed at points in your code to display notes at the command line to find where you are getting up to in your code before it crashes would help.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8766
  • AKA Daniel
Re: insert blocks
« Reply #5 on: January 18, 2008, 10:41:42 PM »
try adding this after tr.Commit()

Code: [Select]
   db.CloseInput(true);
   db.Dispose();

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: insert blocks
« Reply #6 on: January 19, 2008, 12:56:52 AM »
try adding this after tr.Commit()

Code: [Select]
   db.CloseInput(true);
   db.Dispose();

or wrap the transaction with a using statement or 2
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Humbertogo

  • Guest
Re: insert blocks
« Reply #7 on: January 19, 2008, 01:45:26 AM »
okay thanks it work fine

            ''bref.ExplodeToOwnerSpace()
            ''bref.Erase()
why is Block Name: ""

Quote
As I see in ObjectARX SDK docs this method should work
just with uniform scaled blocks
Thus, you don't defined scale factors, it would not work for you

witch method need to use to defined scale factors
« Last Edit: January 19, 2008, 02:05:47 AM by Humbertogo »

Fatty

  • Guest
Re: insert blocks
« Reply #8 on: January 19, 2008, 04:34:08 AM »
okay thanks it work fine

            ''bref.ExplodeToOwnerSpace()
            ''bref.Erase()
why is Block Name: ""


I have a got the same result
I don't know why this happens, I need to play with
this moment extensively
Pephaps, instead of this method better yet
to use just Explode method but I'm not sure
about, sorry

~'J'~