Author Topic: Extents issue  (Read 1962 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Extents issue
« on: April 11, 2013, 02:30:58 PM »
Hi,

I am trying to zoom a viewport, but when I get to this line of code (mExtents.Set(mMinExt, mMaxExt)) it says eInvalidInput. The reason is because db.Extmax and db.Extmin are returning values in this format...

1.0E+20

What would be causing this to happen?

Thanks,

-Ted

TJK44

  • Guest
Re: Extents issue
« Reply #1 on: April 11, 2013, 03:54:49 PM »
Ok, I got that figured out. I was using the template database rather than the database the which contained the geometry.

I am trying to create a batch that will cycle through all dwgs in a selected folder, wblock the dwg to a template file, zoom extents the viewport (already exists) in the new file and save. I am having some difficulties with the zoom portion. Borrowed from here

I think the issue is the viewport number is always -1. I'm not sure what I am doing wrong that it is -1 which means its inactive?

Code - Visual Basic: [Select]
  1. <CommandMethod("BatchExport")> _
  2.         Public Sub BatchExport()
  3.             Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  4.             Dim db As Database = doc.Database
  5.             Dim ed As Editor = doc.Editor
  6.  
  7.             Dim fb As New Windows.Forms.FolderBrowserDialog
  8.             fb.Description = "Search for folder containing files needing converted."
  9.             fb.SelectedPath = Path.GetDirectoryName(doc.Name)
  10.  
  11.             If fb.ShowDialog() = Windows.Forms.DialogResult.OK Then
  12.                 Dim folderPath As String = fb.SelectedPath
  13.                 Dim files() As String = Directory.GetFiles(folderPath, "*.dwg")
  14.                 For Each file As String In files
  15.                     Dim dwtDB As New Database(False, False)
  16.                     Dim newFile As String = ""
  17.                     dwtDB.ReadDwgFile("\\fileserver\TEMPLATE_20130408.dwt", FileShare.ReadWrite, False, "")
  18.  
  19.                     Dim dwgDB As New Database(False, False)
  20.                     dwgDB.ReadDwgFile(file, FileShare.ReadWrite, False, "")
  21.                     Dim objCol As New ObjectIdCollection()
  22.                     Using tr As Transaction = dwgDB.TransactionManager.StartTransaction
  23.                         Dim bt As BlockTable = tr.GetObject(dwgDB.BlockTableId, OpenMode.ForRead, False)
  24.                         Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
  25.                         For Each id As ObjectId In btr
  26.                             objCol.Add(id)
  27.                         Next
  28.                     End Using
  29.  
  30.  
  31.                     dwgDB.Wblock(dwtDB, objCol, Point3d.Origin, DuplicateRecordCloning.Ignore)
  32.  
  33.                     Using extTr As Transaction = dwtDB.TransactionManager.StartTransaction()
  34.                         HostApplicationServices.WorkingDatabase = dwtDB
  35.                         Dim layoutMgr As LayoutManager = LayoutManager.Current
  36.                         ed.WriteMessage("Number of layouts = {0}" & vbLf, layoutMgr.LayoutCount)
  37.                         ed.WriteMessage("Current layout = {0}" & vbLf, layoutMgr.CurrentLayout)
  38.                         Using layoutDict As DBDictionary = extTr.GetObject(dwtDB.LayoutDictionaryId, OpenMode.ForRead)
  39.                             For Each layoutEntry As DictionaryEntry In layoutDict
  40.                                 Using layoutObj As Layout = TryCast(extTr.GetObject(DirectCast(layoutEntry.Value, ObjectId), OpenMode.ForRead), Layout)
  41.                                     If Not layoutObj.LayoutName.ToUpper = "MODEL" Then
  42.                                         layoutMgr.CurrentLayout = layoutObj.LayoutName
  43.                                         Dim r As BlockTableRecord = extTr.GetObject(layoutObj.BlockTableRecordId, OpenMode.ForRead)
  44.                                         For Each obj As ObjectId In r
  45.                                             Dim dbObj As DBObject = extTr.GetObject(obj, OpenMode.ForRead)
  46.                                             Dim vp As Autodesk.AutoCAD.DatabaseServices.Viewport = TryCast(dbObj, Autodesk.AutoCAD.DatabaseServices.Viewport)
  47.                                             If vp IsNot Nothing Then
  48.                                                 ed.WriteMessage(vbLf & "number of Viewport = {0}", vp.Number)
  49.  
  50.  
  51.  
  52.                                                 ' get the screen aspect ratio to calculate
  53.  
  54.                                                 ' the height and width
  55.  
  56.                                                 Dim mScrRatio As Double
  57.  
  58.                                                 ' width/height
  59.  
  60.                                                 mScrRatio = (vp.Width / vp.Height)
  61.  
  62.  
  63.                                                 Dim mMaxExt As Point3d = dwgDB.Extmax
  64.  
  65.                                                 Dim mMinExt As Point3d = dwgDB.Extmin
  66.  
  67.  
  68.  
  69.                                                 Dim mExtents As New Extents3d()
  70.  
  71.                                                 mExtents.Set(mMinExt, mMaxExt)
  72.  
  73.  
  74.  
  75.                                                 ' prepare Matrix for DCS to WCS transformation
  76.  
  77.                                                 Dim matWCS2DCS As Matrix3d
  78.  
  79.                                                 matWCS2DCS = Matrix3d.PlaneToWorld(vp.ViewDirection)
  80.  
  81.  
  82.  
  83.                                                 matWCS2DCS = Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) * matWCS2DCS
  84.  
  85.  
  86.  
  87.                                                 matWCS2DCS = Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) * matWCS2DCS
  88.  
  89.                                                 matWCS2DCS = matWCS2DCS.Inverse()
  90.  
  91.  
  92.  
  93.                                                 ' tranform the extents to the DCS
  94.  
  95.                                                 ' defined by the viewdir
  96.  
  97.                                                 mExtents.TransformBy(matWCS2DCS)
  98.  
  99.  
  100.  
  101.                                                 ' width of the extents in current view
  102.  
  103.                                                 Dim mWidth As Double
  104.  
  105.                                                 mWidth = (mExtents.MaxPoint.X - mExtents.MinPoint.X)
  106.  
  107.  
  108.  
  109.                                                 ' height of the extents in current view
  110.  
  111.                                                 Dim mHeight As Double
  112.  
  113.                                                 mHeight = (mExtents.MaxPoint.Y - mExtents.MinPoint.Y)
  114.  
  115.  
  116.  
  117.                                                 ' get the view center point
  118.  
  119.  
  120.  
  121.                                                 Dim mCentPt As New Point2d(((mExtents.MaxPoint.X + mExtents.MinPoint.X) * 0.5), ((mExtents.MaxPoint.Y + mExtents.MinPoint.Y) * 0.5))
  122.  
  123.  
  124.  
  125.                                                 ' check if the width 'fits' in current window,
  126.  
  127.                                                 ' if not then get the new height as
  128.  
  129.                                                 ' per the viewports aspect ratio
  130.  
  131.                                                 If mWidth > (mHeight * mScrRatio) Then
  132.  
  133.                                                     mHeight = mWidth / mScrRatio
  134.                                                 End If
  135.  
  136.  
  137.  
  138.                                                 ' set the viewport parameters
  139.  
  140.                                                 If vp.Number = 2 Then
  141.  
  142.  
  143.                                                     vp.UpgradeOpen()
  144.  
  145.                                                     ' set the view height - adjusted by 1%
  146.  
  147.                                                     vp.ViewHeight = mHeight * 1.01
  148.  
  149.                                                     ' set the view center
  150.  
  151.                                                     vp.ViewCenter = mCentPt
  152.  
  153.                                                     vp.Visible = True
  154.  
  155.                                                     vp.On = True
  156.  
  157.                                                     vp.UpdateDisplay()
  158.  
  159.                                                     'ed.SwitchToModelSpace()
  160.  
  161.  
  162.                                                     Application.SetSystemVariable("CVPORT", vp.Number)
  163.                                                 End If
  164.  
  165.                                                 If vp.Number = 3 Then
  166.  
  167.  
  168.                                                     vp.UpgradeOpen()
  169.  
  170.                                                     vp.ViewHeight = mHeight * 1.25
  171.  
  172.                                                     'set the view center
  173.  
  174.                                                     vp.ViewCenter = mCentPt
  175.  
  176.                                                     vp.Visible = True
  177.  
  178.                                                     vp.On = True
  179.  
  180.                                                     vp.UpdateDisplay()
  181.  
  182.                                                     'ed.SwitchToModelSpace()
  183.  
  184.  
  185.                                                     Application.SetSystemVariable("CVPORT", vp.Number)
  186.  
  187.                                                 End If
  188.                                             End If
  189.                                         Next
  190.                                     End If
  191.                                 End Using
  192.                             Next
  193.                         End Using
  194.                         extTr.Commit()
  195.                     End Using
  196.  
  197.                     Try
  198.                         newFile = folderPath & "\_" & Path.GetFileName(file)
  199.                         dwtDB.SaveAs(newFile, DwgVersion.AC1024)
  200.                     Catch ex As System.Exception
  201.                         Application.ShowAlertDialog(ex.Message)
  202.                     End Try
  203.                     dwtDB.Dispose()
  204.                     dwgDB.Dispose()
  205.                     HostApplicationServices.WorkingDatabase = db
  206.                 Next
  207.             End If
  208.  
  209.         End Sub
  210.  
« Last Edit: April 11, 2013, 04:02:31 PM by TJK44 »

TJK44

  • Guest
Re: Extents issue
« Reply #2 on: April 12, 2013, 09:57:35 AM »
I guess my main question would be, does a viewport have to be active in order to manipulate it? If so, how would I make it active without opening the drawing in autocad?
« Last Edit: April 12, 2013, 10:09:58 AM by TJK44 »