Author Topic: BlockPreview in PictureBox  (Read 1889 times)

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
BlockPreview in PictureBox
« on: December 23, 2012, 10:18:19 AM »
Hello,

I am trying to show a Preview of a block in a form using a picturebox.

I have a listbox containing the blocknames of the active drawing, and show a preview of the selected block in a Picturebox using de Blockrecord.PreviewIcon.
But the quality of the preview is bad. How can i get a preview like the one in the "Block Edit" Form, or like the "Object Viewer" in AutoCAD?


         


Can someone help me with this?


mgreven

  • Guest
Re: BlockPreview in PictureBox
« Reply #1 on: December 23, 2012, 06:12:50 PM »
I have made a routine which makes a snapshot of a block and saves this to a file, this file i can use in the picturebox.
But there must be a better and quicker way to do it.

Here is my code:

Code - vb.net: [Select]
  1. Public Function GetSnapshot(Blockname As String) As Bitmap
  2.         Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  3.         Dim ed As Editor = doc.Editor
  4.         Dim db As Database = doc.Database
  5.         Dim gsm As Manager = doc.GraphicsManager
  6.         Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
  7.         Dim MyBitmap As New System.Drawing.Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, Imaging.PixelFormat.Format32bppPArgb)
  8.         MyBitmap.SetResolution(200, 200)
  9.         ' Get some AutoCAD system variables
  10.         Dim vpn As Integer = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))
  11.         ' Get AutoCAD's GS view for this document...
  12.         Dim gsv As View = doc.GraphicsManager.GetGsView(vpn, True)
  13.  
  14.  
  15.         ' ... but create a new one for the actual snapshot
  16.         Using view As New View()
  17.             ' Set the view to be just like the one
  18.             ' in the AutoCAD editor
  19.             view.Viewport = gsv.Viewport
  20.             view.SetView(gsv.Position, gsv.Target, gsv.UpVector, gsv.FieldWidth, gsv.FieldHeight)
  21.             view.EraseAll()
  22.             ' Set the visual style to the one passed in
  23.             view.VisualStyle = New VisualStyle(VisualStyleType.Wireframe2D)
  24.             Dim dev As Device = gsm.CreateAutoCADOffScreenDevice()
  25.             Using dev
  26.                 dev.OnSize(gsm.DisplaySize)
  27.                 ' Set the render type and the background color
  28.                 dev.DeviceRenderType = RendererType.[Default]
  29.                 dev.BackgroundColor = Color.Black
  30.                 ' Add the view to the device and update it
  31.                 dev.Add(view)
  32.                 dev.Update()
  33.                 Using model As Model = gsm.CreateAutoCADModel()
  34.                     Dim tr As Transaction = db.TransactionManager.StartTransaction()
  35.                     Dim BlkRefID As ObjectId
  36.                     Using tr
  37.                         ' Add the modelspace to the view
  38.                         ' It's a container but also a drawable
  39.                         Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  40.                         ''Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt("R-12000-7250 A U is 0"), OpenMode.ForRead), BlockTableRecord)
  41.                         Dim btr As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
  42.                         If bt.Has(Blockname) Then
  43.                             ''Dim dummy As ObjectId = InsertBlock(db, "ModelSpace", New Point3d(0, 0, 0), Blockname, 1, 1, 1, 0)
  44.                             BlkRefID = InsertBlock(db, "ModelSpace", New Point3d(0, 0, 0), Blockname, 1, 1, 1, 0)
  45.                         End If
  46.  
  47.                         view.Add(btr, model)
  48.                         tr.Commit()
  49.  
  50.                        
  51.  
  52.                        
  53.                     End Using
  54.                     ''Dim acadApp As Object = Application.AcadApplication
  55.                     ''acadApp.ZoomExtents(New Point3d(0, 0, 0), New Point3d(7500, 13000, 0))
  56.                     view.ZoomExtents(New Point3d(0, 0, 0), New Point3d(7500, 13000, 0))
  57.                     ' Take the snapshot
  58.                     Dim rect As Rectangle = view.Viewport
  59.  
  60.                     Using bitmap As Bitmap = view.GetSnapshot(rect)
  61.                         MyBitmap = bitmap
  62.  
  63.                         bitmap.Save("c:\temp\" & Blockname & ".png")
  64.  
  65.                         ''MyBitmap = bitmap
  66.                         ''bitmap.Save(filename)
  67.                         ''ed.WriteMessage(vbLf & "Snapshot image saved to: " & filename)
  68.                         ' Clean up
  69.                         ''view.EraseAll()
  70.                         dev.[Erase](view)
  71.                         ''Return bitmap
  72.                     End Using
  73.  
  74.                     Dim Mytr As Transaction = db.TransactionManager.StartTransaction()
  75.                     Using Mytr
  76.  
  77.                         Dim bt As BlockTable = DirectCast(Mytr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
  78.                         Dim btr As BlockTableRecord = DirectCast(Mytr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  79.  
  80.                         Dim MyBlockRef As AcadDb.BlockReference = Mytr.GetObject(BlkRefID, OpenMode.ForWrite)
  81.                         MyBlockRef.Erase()
  82.                         Mytr.Commit()
  83.  
  84.                     End Using
  85.                 End Using
  86.  
  87.             End Using
  88.             view.Dispose()
  89.         End Using
  90.  
  91.         Return MyBitmap
  92.  
  93.     End Function
  94.  
  95.