Using newDb As Database = New Database(False, False)
newDb.ReadDwgFile(strTemplatePath, FileOpenMode.OpenForReadAndWriteNoShare, True, Nothing)
newDb.CloseInput(True)
Dim newDbMsId As ObjectId = SymbolUtilityServices.GetBlockModelSpaceId(newDb)
Dim idMap As IdMapping = New IdMapping()
db.WblockCloneObjects(idColl, newDbMsId, idMap, DuplicateRecordCloning.Replace, False)
Dim prevDb As Database = HostApplicationServices.WorkingDatabase
HostApplicationServices.WorkingDatabase = newDb
newDb.UpdateExt(True)
newDb.TileMode = True
Using vTab As ViewportTable = TryCast(newDb.ViewportTableId.Open(OpenMode.ForRead), ViewportTable)
Dim acVptId As ObjectId = vTab("*Active")
Using vpTabRec As ViewportTableRecord = TryCast(acVptId.Open(OpenMode.ForWrite), ViewportTableRecord)
Dim scrRatio As Double = (vpTabRec.Width / vpTabRec.Height)
Dim matWCS2DCS As Matrix3d = Matrix3d.PlaneToWorld(vpTabRec.ViewDirection)
matWCS2DCS = Matrix3d.Displacement(vpTabRec.Target - Point3d.Origin) * matWCS2DCS
matWCS2DCS = Matrix3d.Rotation(-vpTabRec.ViewTwist, vpTabRec.ViewDirection, vpTabRec.Target) * matWCS2DCS
matWCS2DCS = matWCS2DCS.Inverse()
Dim extents As Extents3d = New Extents3d(newDb.Extmin, newDb.Extmax)
extents.TransformBy(matWCS2DCS)
Dim width As Double = (extents.MaxPoint.X - extents.MinPoint.X)
Dim height As Double = (extents.MaxPoint.Y - extents.MinPoint.Y)
Dim center As Point2d = New Point2d((extents.MaxPoint.X + extents.MinPoint.X) * 0.5, (extents.MaxPoint.Y + extents.MinPoint.Y) * 0.5)
If width > (height * scrRatio) Then height = width / scrRatio
vpTabRec.Height = height
vpTabRec.Width = height * scrRatio
vpTabRec.CenterPoint = center
End Using
End Using
HostApplicationServices.WorkingDatabase = prevDb
newDb.SaveAs(strSave, DwgVersion.AC1021)
End Using