Author Topic: Possible to Insert Block from Embedded Resource?  (Read 4828 times)

0 Members and 1 Guest are viewing this topic.

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: Possible to Insert Block from Embedded Resource?
« Reply #15 on: June 25, 2013, 02:37:38 AM »
Yes it is possible and it is quite easy to do. I use it a lot in my projects so users don't need special templates or block collections and still have a good look and feel.

What to do? Collect your blocks you want to use. Keep in mind to save them in the lowest dwg format as the AutoCAD version your application runs in. If you have more blocks to share, put them in one dwg file (i.e. with the name YourAppCollection.dwg). Add that file to your resources.

Then you can run the following code to insert the file into the current drawing.

Code: [Select]
  Sub AddDefaultBlocks()
    Dim doc As acAppServ.Document = acAppServ.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database

    Dim sTempLocation As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\"

    Dim tr As Transaction = db.TransactionManager.StartTransaction()
    Using tr
      If Not CurrentAvailableBlocks.Contains("MyAppBlocksCollection") Then
        Dim sLocation As String = sTempLocation & "MyAppBlocksCollection.dwg"
        My.Computer.FileSystem.WriteAllBytes(sLocation, My.Resources.MyAppBlocksCollection, False)
        If IO.File.Exists(sLocation) = False Then Return
        Using destdb As Database = New Database(False, True)
          destdb.ReadDwgFile(sLocation, IO.FileShare.Read, True, "")
          Dim btrID As ObjectId = db.Insert("MyAppBlocksCollection", destdb, False)
        End Using
      End If

      tr.Commit()
    End Using

  End Sub

Assuming you collected a list of available blocks in a list called CurrentAvailableBlocks, to check if the block definition already exist.

After inserting your block or your block collection you are able to use the blocks that are imported. Also your users can prepare their template with these blocks in their own style. If the blocks exist then your application uses their blocks, else yours.
« Last Edit: June 25, 2013, 02:41:29 AM by huiz »
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Possible to Insert Block from Embedded Resource?
« Reply #16 on: June 25, 2013, 02:58:25 AM »
I'd consider serializing the whole BlockTableRecord instead of a .dwg file to acheive this.  Then you could pull the BTR straight out of your resource and add to the block table of your liking.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Possible to Insert Block from Embedded Resource?
« Reply #17 on: June 25, 2013, 05:03:57 AM »
Thanks Jeff/Kerry :)

Actually there was a nice little snippit in the swamp that solved the problem I was struggling with :) wish I could link to it but I already lost it lol...I was trying to run an if/else statement to check if my block was in the drawing, if it was it would run one function but if wasn't it was supposed to retreive it from a directory. For some reason, the two functions worked great individually, but together they would not cooperate. I got tired of trying to figure out why and came here :) The one I found here was much better, a combined version, with both your names all over it the page if I remember right :)

Thanks!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Possible to Insert Block from Embedded Resource?
« Reply #18 on: June 25, 2013, 05:09:22 AM »
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.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Possible to Insert Block from Embedded Resource?
« Reply #19 on: June 25, 2013, 05:29:26 AM »
Perhaps somewhere in this lot
http://www.theswamp.org/index.php?topic=37686.msg427184#msg427184

That was the one! :) ... but now Huiz and Will has me curious...dang it lol... I'll never sleep

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Possible to Insert Block from Embedded Resource?
« Reply #20 on: June 25, 2013, 05:31:28 AM »
Yes it is possible and it is quite easy to do. I use it a lot in my projects so users don't need special templates or block collections and still have a good look and feel.

What to do? Collect your blocks you want to use. Keep in mind to save them in the lowest dwg format as the AutoCAD version your application runs in. If you have more blocks to share, put them in one dwg file (i.e. with the name YourAppCollection.dwg). Add that file to your resources.

Then you can run the following code to insert the file into the current drawing.

Code: [Select]
  Sub AddDefaultBlocks()
    Dim doc As acAppServ.Document = acAppServ.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database

    Dim sTempLocation As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\"

    Dim tr As Transaction = db.TransactionManager.StartTransaction()
    Using tr
      If Not CurrentAvailableBlocks.Contains("MyAppBlocksCollection") Then
        Dim sLocation As String = sTempLocation & "MyAppBlocksCollection.dwg"
        My.Computer.FileSystem.WriteAllBytes(sLocation, My.Resources.MyAppBlocksCollection, False)
        If IO.File.Exists(sLocation) = False Then Return
        Using destdb As Database = New Database(False, True)
          destdb.ReadDwgFile(sLocation, IO.FileShare.Read, True, "")
          Dim btrID As ObjectId = db.Insert("MyAppBlocksCollection", destdb, False)
        End Using
      End If

      tr.Commit()
    End Using

  End Sub

Assuming you collected a list of available blocks in a list called CurrentAvailableBlocks, to check if the block definition already exist.

After inserting your block or your block collection you are able to use the blocks that are imported. Also your users can prepare their template with these blocks in their own style. If the blocks exist then your application uses their blocks, else yours.

Thanks for sharing this :)