TheSwamp
Code Red => .NET => Topic started by: TJK44 on January 04, 2012, 01:41:21 PM
-
I am trying to xref a drawing a user selects into a template we have and then perform a saveas to another directory. The file shows up in the xref manager in the newly created drawing file but says it is unresolved. I have tried using db.ResolveXRefs but this does not seem to do anything. The created file is never opened so I am trying to do this by reading the database, not sure if that is possible. Here is my code if anyone could take a look and maybe I am missing something easy.
Thanks,
Ted
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim document As Document = Application.DocumentManager.MdiActiveDocument
Dim dir As String = Path.GetDirectoryName(document.Name)
Dim dirConst As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction"
Try
For Each obj As Object In New System.Collections.ArrayList(lstConstruction.Items)
Dim fileName As String = Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Parts" & "\" & obj.ToString
Dim db As Database = New Database(False, True)
db.ReadDwgFile("Y:\CAD STANDARDS\Blocks\Templates\CONSTRUCTION 2012 EPICOR.dwt", FileShare.ReadWrite, False, "")
Using trx As Transaction = db.TransactionManager.StartTransaction
Dim bt2 As BlockTable = trx.GetObject(db.BlockTableId, OpenMode.ForWrite)
Dim btrMs2 As BlockTableRecord = trx.GetObject(bt2(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim xRid As ObjectId = db.AttachXref(fileName, Path.GetFileNameWithoutExtension(fileName))
Dim bref As New BlockReference(Point3d.Origin, xRid)
btrMs2.AppendEntity(bref)
Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(fileName)
'Get the layer table first...
Dim lt As LayerTable = trx.GetObject(db.LayerTableId, OpenMode.ForRead)
Dim layerId As ObjectId = ObjectId.Null
'Check if EmployeeLayer exists...
If lt.Has(blname) Then
layerId = lt.Item(blname)
Else
'If not, create the layer here.
lt.UpgradeOpen()
Dim ltr As LayerTableRecord = New LayerTableRecord()
ltr.Name = blname ' Set the layer name
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
layerId = lt.Add(ltr)
trx.AddNewlyCreatedDBObject(ltr, True)
End If
bref.LayerId = layerId
trx.AddNewlyCreatedDBObject(bref, True)
db.ResolveXrefs(True, True)
trx.Commit()
db.SaveAs(Path.GetDirectoryName(document.Name) & "\" & Path.GetFileNameWithoutExtension(document.Name) & " - Construction\" & obj.ToString, DwgVersion.Current)
End Using
Next
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
-
Lets see if I am on the right track:
1. Select a file for XRef
2. Open a template in AutoCAD
3. Xref the file
4. Save ThisDrawing as ....
-
I am using 2004 but it looks like something like this SHOULD work unless AutoDesk did something screwy.
Dim strTemplateFullName As String = "C:\Documents and Settings\bhardin\Local Settings\Application Data\Autodesk\AutoCAD 2004\R16.0\enu\Template\JIS A4 (portrait) -Named Plot Styles.dwt"
Dim strPath As String = "C:\MyDrawing.dwg"
Dim strMyXref As String = "C:\MyXRef.dwg"
Dim insPoint As Object
objDWG = AcadApp.Documents.Open(strTemplateFullName)
objDWG.Utility.CreateTypedArray(insPoint, vbDouble, 0, 0, 0)
objDWG.ModelSpace.AttachExternalReference(strMyXref, "MyXRef", insPoint, 1, 1, 1, 0, False)
'Add Xref
objDWG.SaveAs(strPath, AutoCAD.AcSaveAsType.acNative)
-
Yes, the user selects the file they want to xref. I then want to read the dwt file and add an external reference and saveas to new location and filename. It creates the new file with the xref, the xref just shows as unresolved.
-
Does the code I wrote work with your paths and some minor changes to the variables?
Such as AcadApp = Application & objDWG = document
Try hard coding your paths first.
-
Chuck,
What does Utility refer to?
-
What does Utility refer to?
Supposedly to a property of AcadDocumentClass in VBA/Interop, accessing a "series of methods provided for utility purposes". In .Net there's no such thing; most of that methods can be found in the EditorInput namespace.
Ted, I think you need to
1) call Dispose on your self-created Database objects or wrap them in a Using statement,
2) call ResolveXrefs after committing the Transaction, and
3) disregard people who do not contribute to the discussion.
Cheers
-
Thanks kaefer, I will get around to trying this sometime today.
-
Yeah thanks kaefer guess I will keep my code to myself till work upgrades my AutoCAD.