Insert external DWG file as block using support file search path locations

Insert external DWG file as block using support file search path locations

Anonymous
Not applicable
2,990 Views
2 Replies
Message 1 of 3

Insert external DWG file as block using support file search path locations

Anonymous
Not applicable

I have a .NET app that I wrote and pieced together from the internet. My app works in AutoCAD 2012 no problem. But we're moving to AutoCAD 2015 and I had to change the DLL files that are referenced to the 2015 files. Most of the app works fine.

 

The app checks the current Database if a certain block exists. If the block does exist, it reads all of the paperspace tabs and reads the titleblock in each tab and stores the drawing numbers, sheet numbers and titles from each. It then updates the attributes of a "Drawing List" block. If the block doesn't exist it inserts the block into the drawing where the user selected. And then updates the attributes as described above.

 

The function I use to insert the block and where the error is happening is:

 

Public Function InsertBlockReference(db As Database, blockName As String, insertPoint As Point3d, scale As Scale3d, angle As Double, layer As String, _
    attValues As System.Collections.Hashtable) As ObjectId
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            If Not bt.Has(blockName) Then
                Dim blockPath As String = HostApplicationServices.Current.FindFile(blockName & Convert.ToString(".dwg"), db, FindFileHint.[Default])
                If String.IsNullOrEmpty(blockPath) Then
                    Return ObjectId.Null
                End If
                bt.UpgradeOpen()
                Using tmpDb As New Database(False, True)
                    tmpDb.ReadDwgFile(blockPath, FileShare.Read, True, Nothing)
                    db.Insert(blockName, tmpDb, True)
                End Using
            End If
            Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt(blockName), OpenMode.ForRead), BlockTableRecord)

            Using br As New BlockReference(insertPoint, btr.ObjectId) With {.ScaleFactors = scale, .Rotation = angle, .Layer = layer}
                Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
                br.TransformBy(ed.CurrentUserCoordinateSystem)

                br.RecordGraphicsModified(True)
                If btr.Annotative = AnnotativeStates.[True] Then
                    Dim contextCollection As ObjectContextCollection = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES")
                    Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(br, contextCollection.GetContext("1:1"))
                End If
                TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord).AppendEntity(br)
                For Each id As ObjectId In btr
                    Dim aDef As AttributeDefinition = TryCast(tr.GetObject(id, OpenMode.ForRead), AttributeDefinition)
                    If aDef IsNot Nothing Then
                        Using aRef As New AttributeReference()
                            aRef.SetAttributeFromBlock(aDef, br.BlockTransform)
                            aRef.Position = aDef.Position + br.Position.GetAsVector()
                            If attValues.ContainsKey(aDef.Tag.ToUpper()) Then
                                aRef.TextString = attValues(aDef.Tag.ToUpper()).ToString()
                            End If
                            br.AttributeCollection.AppendAttribute(aRef)
                            tr.AddNewlyCreatedDBObject(aRef, True)
                        End Using
                    End If
                Next
                tr.AddNewlyCreatedDBObject(br, True)
                tr.Commit()
                Return br.ObjectId
            End Using
        End Using
    End Function

I got the above Function from a forum online and I can't seem to find it again. I'll update if I can find it.

 

The line that seems to have the issue is:

 

Dim blockPath As String = HostApplicationServices.Current.FindFile(blockName & Convert.ToString(".dwg"), db, FindFileHint.[Default])

 And the error I'm receiving is:

 

 

Autodesk.AutoCAD.Runtime.Exception: eFilerError
   at Autodesk.AutoCAD.DatabaseServices.ImpHostApplicationServices.FindFile(String fileName, Database database, FindFileHint hint)
   at DrawingList.Class1.InsertBlockReference(Database db, String blockName, Point3d insertPoint, Scale3d scale, Double angle, String layer, Hashtable attValues) in \DrawingList - 2015\DrawingList\Class1.vb:line 541
   at DrawingList.Class1.newDrawingList() in \DrawingList\Class1.vb:line 84
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
   at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

 

 

And I can't seem to figure out why this is happening. Any help would be greatly appreciated.

0 Likes
Accepted solutions (1)
2,991 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

The FindFile() method throws an exception (eFileError) if the file is not found in the search paths.

Try replacing:

 

Dim blockPath As String = HostApplicationServices.Current.FindFile(blockName & Convert.ToString(".dwg"), db, FindFileHint.[Default])
If String.IsNullOrEmpty(blockPath) Then
    Return ObjectId.Null
 End If

with something like this (not sure about VB syntax):

 

Dim blockPath As String
Try
blockPath = HostApplicationServices.Current.FindFile(blockName & Convert.ToString(".dwg"), db, FindFileHint.[Default]) Catch ex As Exception
MessageBox.Show(ex.Message) Return ObjectId.Null End Try

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

Anonymous
Not applicable

Thank you!!! I thought I had the correct path in my Support File Search Path and I didn't. I had even checked it but I missed the last sub folder. I'm going to add the error catch to the program as soon as I can.

0 Likes