Author Topic: Trying to insert block in current UCS  (Read 14830 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Trying to insert block in current UCS
« Reply #30 on: March 30, 2011, 04:11:43 PM »
Don't make VB more verbose than it is:
Dim btrId As ObjectId = bt(blockname).GetObject(OpenMode.ForRead).ObjectId
is the same as:
Dim btrId As ObjectId = bt(blockname)
anyway, you can throw this expression as you never use btrId...

IMO, rather than trying to integrate the code I posted to yours, translate it to VB, try it, then add the features you want one by one, trying and debuging the code at each step.
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Trying to insert block in current UCS
« Reply #31 on: March 30, 2011, 04:14:28 PM »
By the name of your method I would assume you want insert another drawing as a block?

blkid = curdb.Insert(path, db, True)


Would you like to start fresh with option strict off so you do not need all the directcast?


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trying to insert block in current UCS
« Reply #32 on: March 30, 2011, 04:19:11 PM »

Also ..
It may help if you included the sub/function that you are calling this from
... would make it so much easier to debug explicitly for your situation.
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.

Patch61

  • Guest
Re: Trying to insert block in current UCS
« Reply #33 on: March 30, 2011, 04:19:39 PM »
Option strict *is* off... Directcast is something I know NOTHING about, anyway. I got that from somebody's example or a conversion from C#.

I am hating .Net more and more. I used to love programming. Since .Net it has been headache after headache.

Here is the sub I am calling from (a button click - the BIC is a class that includes my block handling routines):

Code: [Select]
  Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
      'Dim InsStatus As Integer
      Me.Hide()
      Dim pPtRes As PromptPointResult
      Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
      Dim DWGname As String
      Dim InsSpace As String = "Model"
      Dim eXplode As Boolean = False
      Dim InsLayer As String = "Misc"
      Dim strTemp As String
      Dim acDoc As Document = AcApp.DocumentManager.MdiActiveDocument

      CurDWG = TreeView1.SelectedNode.Tag
      DWGname = IO.Path.GetFileNameWithoutExtension(CurDWG)

      If My.Computer.Keyboard.ShiftKeyDown Then
         OpenFile(CurDWG, False)
      Else

         If VBStr.Right(DWGname, 3) = "-ps" Then
            'DWGname = VBStr.Left(DWGname, Len(DWGname) - 3)
            InsSpace = "Paper"
            InsLayer = "Misc"
         Else
            ' Determine Layer, Space, and eXplode
            strTemp = VBStr.Left(VBStr.Right(DWGname, 6), 5)
            If VBStr.Right(DWGname, 1) = "L" And IsNumeric(strTemp) Then
               eXplode = True
            End If
            InsSpace = "Model"
            InsLayer = "Misc"
            strTemp = VBStr.Left(VBStr.Right(DWGname, 6), 5)
         End If
         ChangeSpace(InsSpace)
         'Insert the drawing as a block
         '' Prompt for the start point
         pPtOpts.Message = vbLf & "Click where you wish to insert the block reference: "
         pPtRes = CurDoc.Editor.GetPoint(pPtOpts)
         Dim ptIns As Point3d = pPtRes.Value
         '' Exit if the user presses ESC or cancels the command
         If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
         LYR.SetLayerCurrent(InsLayer)
         BIC.InsertDrawingAsBlock(CurDoc, CurDWG, DWGname, ptIns, InsSpace, InsLayer, eXplode)
      End If

      Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt()
   End Sub

« Last Edit: March 30, 2011, 04:23:59 PM by Patch61 »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Trying to insert block in current UCS
« Reply #34 on: March 30, 2011, 04:25:55 PM »
Quote
I am hating .Net more and more. I used to love programming.
Take the LISP route...
Speaking English as a French Frog

Patch61

  • Guest
Re: Trying to insert block in current UCS
« Reply #35 on: March 30, 2011, 04:27:34 PM »
Quote
I am hating .Net more and more. I used to love programming.
Take the LISP route...

Been there, done that. Have a U.S. Patent for artificially intelligent engineering software I wrote in Lisp.
I WILL NOT GO BACK!!!!

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Trying to insert block in current UCS
« Reply #36 on: March 30, 2011, 04:28:39 PM »
I do not see CurDoc declared, but who cares.

Would like to start off with a simple command to insert a external drawing and from that add options you want and tie in with your form or palette?

Patch61

  • Guest
Re: Trying to insert block in current UCS
« Reply #37 on: March 31, 2011, 09:38:11 AM »
Dim curdb As Database = doc.Database

  Dim db As Database = doc.Database
            Using db
               db.ReadDwgFile(path, System.IO.FileShare.Read, True, "")


Just looking real quick you are using the same database from whatever doc is passed in as a parameter
You need to create a new database before you call ReadDwgFile

Ok, I'm working on doing the 'reverse integration' as you suggested. One thing I don't get is the comment above. Why do I need to create a new database when, as in the above example, I just created one? That doesn't make any sense to me at all. I must be missing something here... again.  :oops: And what does the doc passed to the routine have to do with it?
« Last Edit: March 31, 2011, 10:36:18 AM by Patch61 »

Patch61

  • Guest
Re: Trying to insert block in current UCS
« Reply #38 on: March 31, 2011, 11:07:29 AM »
I do not see CurDoc declared, but who cares.

Would like to start off with a simple command to insert a external drawing and from that add options you want and tie in with your form or palette?

Yes, I would like to start off with a simple command to insert an external drawing. Right now, all I can do is copy what I have, which already has problems.

<rant>
The biggest problem I have with AutoCAD's .Net API is the very crappy documentation. This has to be the worst API documentation I've ever had to use. I've tried to look up how to insert a block and found nothing. Same for inserting a drawing as a block. Where are the step by step instructions? Where is the example? All I can find is technobabble that refers to other technobabble. It is only really useful if you already understand it.
</rant>

Thanks,
Steve
« Last Edit: March 31, 2011, 11:18:55 AM by Patch61 »

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Trying to insert block in current UCS
« Reply #39 on: March 31, 2011, 07:23:58 PM »
Sorry I have been busy or actually sleeping and hanging with kid I will post example later tonight with hopefully good enough notes to understand.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trying to insert block in current UCS
« Reply #40 on: March 31, 2011, 07:31:04 PM »

Steve,
Have a work through this.
It is a work in progress and have all your answers in due course.

http://www.theswamp.org/index.php?topic=37686.msg427185#new
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.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Trying to insert block in current UCS
« Reply #41 on: April 01, 2011, 01:07:08 AM »
As Kerry posted he has started a great thread that will help more than this.


Quote
Option strict *is* off... Directcast is something I know NOTHING about, anyway. I got that from somebody's example or a conversion from C#.
C# does not allow implicit conversion if it is a narrowing conversion. With VB if option strict is off it will allow a narrow conversion.
So using a tool to convert C# to VB can add DirectCast()-- which basically succeeds if the 2 types have a inheritance relationship and is more efficient than CType()

Why do I need to create a new database when, as in the above example, I just created one?
Not sure what new one was created? Both are assigned to the Database of the Document passed into your function.
Dim curdb As Database = doc.Database
Dim db As Database = doc.Database

Quote
Yes, I would like to start off with a simple command to insert an external drawing

This is copied from here http://forums.augi.com/showthread.php?t=126483

Hopefully if you have any questions someone will explain it to you.
Basically Insert a drawing as a block into current drawing then create and insert a new blockreference from it.
So please ask any questions if do not understand what the code is doing

Code: [Select]
<CommandMethod("InsertDwg")> _
        Public Sub InsertDwg()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim fname As String = "C:\Users\Jeff\Documents\Drawing1.dwg"
            Dim ObjId As ObjectId
            Using trx As Transaction = db.TransactionManager.StartTransaction
                Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                Dim btrMs As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
                Using dbInsert As New Database(False, True)
                    dbInsert.ReadDwgFile(fname, IO.FileShare.Read, True, "")
                    ObjId = db.Insert(Path.GetFileNameWithoutExtension(fname), dbInsert, True)
                End Using
                Dim ppo As New PromptPointOptions(vbCrLf & "Insertion Point")
                Dim ppr As PromptPointResult
                ppr = ed.GetPoint(ppo)
                If ppr.Status <> PromptStatus.OK Then
                    ed.WriteMessage(vbCrLf & "You decided to QUIT!")
                    Exit Sub
                End If
                Dim insertPt As Point3d = ppr.Value
                Dim bref As New BlockReference(insertPt, ObjId)
                btrMs.AppendEntity(bref)
                trx.AddNewlyCreatedDBObject(bref, True)
                trx.Commit()
            End Using

        End Sub

Patch61

  • Guest
Re: Trying to insert block in current UCS
« Reply #42 on: April 01, 2011, 10:44:45 AM »

Steve,
Have a work through this.
It is a work in progress and have all your answers in due course.

http://www.theswamp.org/index.php?topic=37686.msg427185#new


Kerry,

Great thread! It's a shame AutoDesk didn't give us anything like that, But I am soooo grateful for your assistance and sample code. If I could buy you a drink, I would.


Gile and JeffH,

Thanks for all your help and codes. I don't know what I would have done without your assistance (probably pulled out lots of my few remaining hairs).


You guys will be happy to know that my routine seems to be working and doing everything I need it to. Woot!

If either of you guys ever come to Orlando and want to see Disney, be sure to hit me up. My wife works there and we enjoy a number of perks from that and I wouldn't mind sharing (the perks, not my wife  :-o :-D).

Thanks again, guys!
« Last Edit: April 01, 2011, 11:17:13 AM by Patch61 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trying to insert block in current UCS
« Reply #43 on: April 01, 2011, 10:20:41 PM »
< .. > grateful for your assistance and sample code. If I could buy you a drink, I would.


< .. >
You're welcome Steve.
It's a bit far for me to travel to collect  :-D

In the mean time, I know Mark would appreciate some monetary assistance to maintain the site so we can play here :).

Regards
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.

TJK44

  • Guest
Re: Trying to insert block in current UCS
« Reply #44 on: December 20, 2011, 06:40:31 PM »
Has anyone got the insert to work with a file that contains only xrefs? It does bring in the file, but in the xref manager all the xrefs are Unresolved. I am forced to right click in the status column and select Reload All References. I went through and debugged and it appears it is actually bring in the xrefs at this line. Could anyone give me some advice as to what I am missing. I'll post the entire code I am using.

Code: [Select]
ObjId = db.Insert(Path.GetFileNameWithoutExtension(fname), dbInsert, True)

Code: [Select]
<CommandMethod("ImportPart")> _
Public Sub ImportPart()

                Dim fname As String = "R:\TEST\X-REF MASTER FILES\000022-01-01 TEST\000022-01-01 test drawing.dwg"


                Dim doc As Document = Application.DocumentManager.MdiActiveDocument
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
                'Dim fname As String = "C:\Users\Jeff\Documents\Drawing1.dwg"
                Dim ObjId As ObjectId
                Using trx As Transaction = db.TransactionManager.StartTransaction
                    Using docLoc As DocumentLock = doc.LockDocument
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim btrMs As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
                        Using dbInsert As New Database(False, True)
                            dbInsert.ReadDwgFile(fname, IO.FileShare.Read, False, "")
                            ObjId = db.Insert(Path.GetFileNameWithoutExtension(fname), dbInsert, True)
                        End Using
                        Dim ppo As New PromptPointOptions(vbCrLf & "Insertion Point")
                        Dim ppr As PromptPointResult
                        ppr = ed.GetPoint(ppo)
                        If ppr.Status <> PromptStatus.OK Then
                            ed.WriteMessage(vbCrLf & "You decided to QUIT!")
                            Exit Sub
                        End If
                        Dim insertPt As Point3d = ppr.Value
                        Dim bref As New BlockReference(insertPt, ObjId)
                        btrMs.AppendEntity(bref)
                        trx.AddNewlyCreatedDBObject(bref, True)

                        trx.Commit()
                        bref.ExplodeToOwnerSpace()


                    End Using
                End Using
            End If

        Catch ex As Exception

        End Try
    End Sub

Thanks,

Ted