Author Topic: Trying to create a 'master' drawing  (Read 1607 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Trying to create a 'master' drawing
« on: February 06, 2012, 06:29:57 PM »
I am getting some weird behavior when I am creating a drawing with just xrefs. I am starting with a drawing that will either have block, xrefs, or a combination of both. The command creates a new blank DWG, goes through each block or xref in the open drawing and then references them to this new blank drawing file. If it is an xref I use the path to xref it in the new drawing. If its a block I wblock it out and then xref this file into the new blank drawing. What's weird is, the wblocked blocks being xrefed in dont maintain their rotation or mirroring, but if it was already xreferenced then it is ok. Here is my code if someone would like to look at it.

Thanks,
Ted
Code - vb.net: [Select]
  1.         <CommandMethod("CreateMasterDWG")> _
  2.         Public Sub MasterTest()
  3.             Dim document As Document = Application.DocumentManager.MdiActiveDocument
  4.             document.SendStringToExecute("_.ucs ", True, False, False)
  5.             document.SendStringToExecute("top ", True, False, False)
  6.             document.SendStringToExecute("_qsave ", True, False, False)
  7.  
  8.             Dim editor As Editor = document.Editor
  9.             Dim database As Database = document.Database
  10.             Dim curdir2 As String = Path.GetDirectoryName(fName2)
  11.                         Dim frm1 As New frmBlockCreator
  12.             Dim strFileName As String
  13.             frm1.SaveFileDialog1.InitialDirectory = curdir2
  14.             frm1.SaveFileDialog1.Title = "Select a location to save the master file."
  15.             frm1.SaveFileDialog1.Filter = "Drawing Files|*.dwg"
  16.             frm1.SaveFileDialog1.AddExtension = True
  17.  
  18.             If frm1.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  19.                 strFileName = frm1.SaveFileDialog1.FileName
  20.             Else
  21.                 Exit Sub
  22.             End If
  23.             Dim db As Database = New Database(True, False)
  24.             Try
  25.                 Using trans2 As Transaction = db.TransactionManager.StartTransaction()
  26.                     db.SaveAs(strFileName, DwgVersion.Newest)
  27.                 End Using
  28.             Catch ex As System.Exception
  29.                 MsgBox(ex.Message)
  30.             End Try
  31.  
  32.             Dim oidc As New ObjectIdCollection()
  33.             Try
  34.                 Using trans As Transaction = database.TransactionManager.StartTransaction()
  35.                     Dim btr As BlockTable = DirectCast(trans.GetObject(database.BlockTableId, OpenMode.ForRead), BlockTable)
  36.                     Dim btrMs As BlockTableRecord = TryCast(btr(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead), BlockTableRecord)
  37.                     'go through each object in the block table record
  38.                     For Each entID As ObjectId In btrMs
  39.                         'create variable used to determine is it is a block reference
  40.                         Dim ent As Entity = trans.GetObject(entID, OpenMode.ForRead, False)
  41.                         'if it is a block then perform code below
  42.                         If TypeOf ent Is BlockReference Then
  43.                             Dim blkRef As BlockReference = TryCast(trans.GetObject(entID, OpenMode.ForRead, False), BlockReference)
  44.  
  45.                             Dim bid As ObjectId = blkRef.BlockTableRecord
  46.                             Dim blocktr As BlockTableRecord = DirectCast(trans.GetObject(bid, OpenMode.ForRead), BlockTableRecord)
  47.  
  48.                             If blocktr.IsFromExternalReference Then
  49.                                 Dim btrpathname As String = blocktr.PathName.ToString
  50.                                 Dim strFileDir As String = Path.GetFileNameWithoutExtension(strFileName)
  51.                                 Dim strSaveDir As String = Path.GetDirectoryName(strFileName)
  52.                                 Dim strBlockName As String = blkRef.Name
  53.                                 Dim db2 As Database = New Database(True, False)
  54.                                 'read newly created dwg file
  55.                                 db2.ReadDwgFile(strFileName, FileShare.ReadWrite, False, "")
  56.                                 Using trx As Transaction = db2.TransactionManager.StartTransaction()
  57.                                     Dim xrefBt As BlockTable = trx.GetObject(db2.BlockTableId, OpenMode.ForWrite)
  58.                                     Dim btrMs2 As BlockTableRecord = trx.GetObject(xrefBt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
  59.  
  60.                                     'create objectID for XRef
  61.                                     Dim xrid As ObjectId = db2.AttachXref(btrpathname, Path.GetFileNameWithoutExtension(btrpathname))
  62.                                     'create block reference, reference the xref objectID
  63.                                     Dim bref As New BlockReference(blkRef.Position, xrid)
  64.                                     'bref.Rotation = blkRef.Rotation
  65.                                     'bref.ScaleFactors = blkRef.ScaleFactors
  66.                                     bref.BlockTransform = blkRef.BlockTransform
  67.  
  68.                                     'append xref to blocktable
  69.                                     btrMs2.AppendEntity(bref)
  70.  
  71.                                     Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(btrpathname)
  72.  
  73.                                     'Get the layer table first...
  74.                                     Dim lt As LayerTable = trx.GetObject(db2.LayerTableId, OpenMode.ForRead)
  75.                                     Dim layerId As ObjectId = ObjectId.Null
  76.                                     'Check if EmployeeLayer exists...
  77.                                     If lt.Has(blname) Then
  78.                                         layerId = lt.Item(blname)
  79.                                     Else
  80.                                         'If not, create the layer here.
  81.                                         lt.UpgradeOpen()
  82.                                         Dim ltr As LayerTableRecord = New LayerTableRecord()
  83.                                         ltr.Name = blname ' Set the layer name
  84.                                         ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
  85.                                         layerId = lt.Add(ltr)
  86.                                         trx.AddNewlyCreatedDBObject(ltr, True)
  87.                                     End If
  88.                                     'trx.Commit()
  89.  
  90.                                     bref.LayerId = layerId
  91.  
  92.  
  93.                                     trx.AddNewlyCreatedDBObject(bref, True)
  94.                                     db2.SaveAs(strFileName, DwgVersion.Current)
  95.                                 End Using
  96.                                 'End If
  97.                             End If
  98.  
  99.                             Dim attRefIds As AttributeCollection = blkRef.AttributeCollection
  100.  
  101.                             'cycle through the attributes associated with the current block
  102.                             For Each attrefid As ObjectId In attRefIds
  103.                                 Dim attref As AttributeReference = trans.GetObject(attrefid, OpenMode.ForRead, False)
  104.  
  105.                                 Select Case attref.Tag
  106.                                     'if block has attribute PartNumber then create the wblock
  107.                                     Case "PartNumber"
  108.                                         Dim strBlockName As String = blkRef.Name
  109.                                         Dim btrid As ObjectId = If(blkRef.IsDynamicBlock, blkRef.DynamicBlockTableRecord, blkRef.BlockTableRecord)
  110.                                         Dim btr2 As BlockTableRecord = DirectCast(trans.GetObject(btrid, OpenMode.ForRead), BlockTableRecord)
  111.  
  112.                                         If btr2.IsLayout OrElse btr2.IsAnonymous OrElse btr2.IsFromExternalReference OrElse btr2.IsFromOverlayReference Then
  113.                                             editor.WriteMessage(vbLf & "Cannot convert block {0} to Xref. ", btr2.Name)
  114.                                         Else
  115.                                             Dim ok As Boolean = False
  116.                                             'Add block objectID
  117.                                             Dim oidc2 As New ObjectIdCollection
  118.                                             oidc2.Add(blkRef.ObjectId)
  119.  
  120.                                             Dim strFileDir As String = Path.GetFileNameWithoutExtension(strFileName)
  121.                                             Dim strSaveDir As String = Path.GetDirectoryName(strFileName)
  122.                                             Dim strFileDirTrim As String = Mid(strFileDir, 1, 4)
  123.  
  124.                                             If Not Directory.Exists(strSaveDir & "\" & strFileDir & " - Views") Then
  125.                                                 Directory.CreateDirectory(strSaveDir & "\" & strFileDir & " - Views")
  126.                                             End If
  127.  
  128.                                             If Not Directory.Exists(strSaveDir & "\" & strFileDir & " - Parts") Then
  129.                                                 Directory.CreateDirectory(strSaveDir & "\" & strFileDir & " - Parts")
  130.                                             End If
  131.  
  132.                                             If Not Directory.Exists(strSaveDir & "\" & strFileDir & " - Construction") Then
  133.                                                 Directory.CreateDirectory(strSaveDir & "\" & strFileDir & " - Construction")
  134.                                             End If
  135.                                             If Not Directory.Exists(strSaveDir & "\" & strFileDir & " - Study") Then
  136.                                                 Directory.CreateDirectory(strSaveDir & "\" & strFileDir & " - Study")
  137.                                             End If
  138.  
  139.                                             Using target As Database = New Database(False, False)
  140.                                                 target.ReadDwgFile("\\fs\drivers\CAD STANDARDS\Blocks\Templates\PART DRAWING 2012 EPICOR.dwt", FileShare.ReadWrite, False, "")
  141.                                                 database.Wblock(target, oidc2, Point3d.Origin, DuplicateRecordCloning.Replace)
  142.                                                 target.SaveAs(strSaveDir & "\" & strFileDir & " - parts\" & strBlockName & ".dwg", DwgVersion.Newest)
  143.  
  144.                                                 ok = True
  145.                                             End Using
  146.  
  147.                                             If ok Then
  148.  
  149.                                                 Dim strPartFile As String = strSaveDir & "\" & strFileDir & " - Parts\" & strBlockName & ".dwg"
  150.                                                 Dim db2 As Database = New Database(False, False)
  151.                                                 'read newly created dwg file
  152.                                                 db2.ReadDwgFile(strFileName, FileShare.ReadWrite, False, "")
  153.                                                 Using trx As Transaction = db2.TransactionManager.StartTransaction()
  154.                                                     Dim xrefBt As BlockTable = trx.GetObject(db2.BlockTableId, OpenMode.ForWrite)
  155.                                                     Dim btrMs2 As BlockTableRecord = trx.GetObject(xrefBt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
  156.  
  157.                                                     'create objectID for XRef
  158.                                                     Dim xrid As ObjectId = db2.AttachXref(strPartFile, Path.GetFileNameWithoutExtension(strPartFile))
  159.                                                     'create block reference, reference the xref objectID
  160.                                                     Dim bref As New BlockReference(blkRef.Position, xrid)
  161.                                                     bref.BlockTransform = blkRef.BlockTransform
  162.  
  163.                                                     btrMs2.AppendEntity(bref)
  164.  
  165.                                                     Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(strPartFile)
  166.  
  167.                                                     'Get the layer table first...
  168.                                                     Dim lt As LayerTable = trx.GetObject(db2.LayerTableId, OpenMode.ForRead)
  169.                                                     Dim layerId As ObjectId = ObjectId.Null
  170.                                                     'Check if EmployeeLayer exists...
  171.                                                     If lt.Has(blname) Then
  172.                                                         layerId = lt.Item(blname)
  173.                                                     Else
  174.                                                         'If not, create the layer here.
  175.                                                         lt.UpgradeOpen()
  176.                                                         Dim ltr As LayerTableRecord = New LayerTableRecord()
  177.                                                         ltr.Name = blname ' Set the layer name
  178.                                                         ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
  179.                                                         layerId = lt.Add(ltr)
  180.                                                         trx.AddNewlyCreatedDBObject(ltr, True)
  181.                                                     End If
  182.                                                     'trx.Commit()
  183.  
  184.                                                     bref.LayerId = layerId
  185.  
  186.  
  187.                                                     trx.AddNewlyCreatedDBObject(bref, True)
  188.                                                     db2.SaveAs(strFileName, DwgVersion.Current)
  189.                                                     trx.Commit()
  190.                                                 End Using
  191.  
  192.                                             End If
  193.  
  194.                                         End If
  195.                                 End Select
  196.  
  197.                             Next
  198.  
  199.                         End If
  200.  
  201.                     Next
  202.  
  203.                 End Using
  204.  
  205.             Catch ex As System.Exception
  206.                 Windows.MessageBox.Show("Error occurred: " & ex.Message, "Error", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
  207.             End Try
  208.  
  209.         End Sub
  210.  
edit:kdub:add formatting
« Last Edit: February 06, 2012, 09:15:13 PM by Kerry »

TJK44

  • Guest
Re: Trying to create a 'master' drawing
« Reply #1 on: February 07, 2012, 11:25:16 AM »
To better explain what I am trying to do.

Basically I have a drawing that has a single block that has been copied, rotated, mirrored, etc. I want to create a new drawing file that looks the exact same, but has only an xref to the original block which I want to wblock out from the original drawing and xref into the new drawing. Hope that makes sense.

TJK44

  • Guest
Re: Trying to create a 'master' drawing
« Reply #2 on: February 13, 2012, 06:38:32 PM »
Haven't looked at this for a while until today. Still not having any luck, if anyone has a suggestion I would appreciate it.

Edit: I attached a file that shows what is happening. The white objects are from the original drawing and the colored objects are what has been generated, They are supposed to be right on top of each other. I have tried using BlockRef.BlockTransform property and other properties  like rotation and scale factors etc. But nothing seems to work.
« Last Edit: February 13, 2012, 06:57:17 PM by TJK44 »