.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ReadDwgFile eNotOpenForWrite

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
JasonSelf
3841 Views, 9 Replies

ReadDwgFile eNotOpenForWrite

I have been diving as deeply as I can into objectarx and .net over the last few weeks, and I ran into another "That didn't work like I expected' issue.

 

This is more of a test than anything else, but I am trying to open a drawing with ReadDwgFile and then pull attribute data from a block in that drawing.

 

Here is my code:

 

Public Sub test()
        Dim pathtodwg As String = "C:\PlotItIn\PDB542503A4.dwg"
        Using dwg As Database = New Database(False, False)
            dwg.ReadDwgFile(pathtodwg, System.IO.FileShare.Read, True, Nothing)
            Dim db As Database = dwg.AcadDatabase
            Using Trans As Transaction = db.TransactionManager.StartTransaction
                Dim curblock As String = "ahlbord" Dim curtag As String = "REV" Dim bktbl As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead) Dim revblk As BlockTableRecord = bktbl(curblock).GetObject(OpenMode.ForRead) Dim refs As ObjectIdCollection = revblk.GetBlockReferenceIds(False, False) For Each ref In refs Dim bref As BlockReference = Trans.GetObject(ref, OpenMode.ForRead) Dim atts As AttributeCollection = bref.AttributeCollection For Each obj In atts Dim att As AttributeReference
                        att = Trans.GetObject(obj, OpenMode.ForRead, False) If att.Tag = curtag Then Application.ShowAlertDialog(att.TextString) End If Next Next End Using End Using End Sub

I get the eNotOpenForWrite and AutoCAD fatals out.  I dropped some alertdialogs in to see how far it was getting and it seems like it's bombing out at the ReadDwgFile line.

 

Thanks,

Jason

 

 

 

9 REPLIES 9
Message 2 of 10
Jeffrey_H
in reply to: JasonSelf

Try

 

 ReadDwgFile(pathtodwg, FileOpenMode.OpenForReadAndReadShare, False, "")

 

You can also find your answers @ TheSwamp
Message 3 of 10
JasonSelf
in reply to: Jeffrey_H

Thanks Jeffrey but That gives me the same result.

Message 4 of 10
Jeffrey_H
in reply to: JasonSelf

This worked for me take the database out of the using block, and a couple of litte changes see code.

 

Here is the code just have to change file name

 

 

 <CommandMethod("Teest")> _
        Public Sub test()
            Dim pathtodwg As String = "C:\Users\Jeff\My Documents\test.dwg"
            Dim db As Database = New Database(False, False)
            db.ReadDwgFile(pathtodwg, System.IO.FileShare.Read, True, Nothing)
            Using Trans As Transaction = db.TransactionManager.StartTransaction
                Dim curblock As String = "ahlbord"
                Dim curtag As String = "REV"
                Dim bktbl As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                Dim revblk As BlockTableRecord = bktbl(curblock).GetObject(OpenMode.ForRead)
                Dim refs As ObjectIdCollection = revblk.GetBlockReferenceIds(False, False)
                For Each ref In refs
                    Dim bref As BlockReference = Trans.GetObject(ref, OpenMode.ForRead)
                    Dim atts As AttributeCollection = bref.AttributeCollection
                    For Each obj In atts
                        Dim att As AttributeReference
                        att = Trans.GetObject(obj, OpenMode.ForRead, False)
                        If att.Tag = curtag Then
                            Application.ShowAlertDialog(att.TextString)
                        End If
                    Next
                Next
            End Using

        End Sub

 

You can also find your answers @ TheSwamp
Message 5 of 10
chiefbraincloud
in reply to: JasonSelf

I believe this is the only offending line of code

 

Dim db As Database = dwg.AcadDatabase

 

you can't do that, because you are taking what is already a Managed Database reference (dwg) retrieving it's COM reference (dwg.AcadDatabase) and setting that to another Managed Database variable (db).  No Can Do.

 

The error I got was not eNotOpenForWrite, so I'm not sure why you got that, but, if I take that line out of your code, and change nothing else except the strings for the filename, blockname, and attribute tag, the code works.  (oh yeah, and replace the references to 'db' with 'dwg')

 

 

Dave O.                                                                  Sig-Logos32.png
Message 6 of 10
JasonSelf
in reply to: chiefbraincloud

Ok, I spoke too soon before.  I had it working once with the code that Jeffrey had written but when I pulled the full code together it stopped working.

 

Here is my code:

 

Public Function DwgQuery(ByVal pathtodwg As String)
        Dim XML_Reader As XML_Reader = New XML_Reader
        Dim XMLFile As XmlDocument
        XMLFile = XML_Reader.XMLRead _
        (System.Windows.Forms.Application.StartupPath & "\CoverIT Config.xml") Dim Confignodelist As XmlNodeList = XMLFile.SelectNodes("/Blocks/Block") Dim revVal As String = Nothing Dim dateVal As String = Nothing Dim desc1Val As String = Nothing Dim desc2Val As String = Nothing Dim desc3Val As String = Nothing Dim desc4Val As String = Nothing Dim db As Database = New Database(False, False) Dim exists As Boolean = File.Exists(pathtodwg) db.ReadDwgFile(pathtodwg, System.IO.FileShare.Read, True, Nothing) Using Trans As Transaction = db.TransactionManager.StartTransaction Dim bktbl As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead) Dim found As Boolean = False For Each Confignode As XmlNode In Confignodelist If found = False Then Dim blockname As String =Confignode.Attributes. _
                    GetNamedItem("name").Value Dim revtag As String = Confignode("Revision").Value Dim datetag As String = Confignode("Date").Value Dim desc1tag As String = Confignode("Desc1").Value Dim desc2tag As String = Confignode("Desc2").Value Dim desc3tag As String = Confignode("Desc3").Value Dim desc4tag As String = Confignode("Desc4").Value Dim revblk As BlockTableRecord =bktbl(blockname). _
                    GetObject(OpenMode.ForRead) Dim refs As ObjectIdCollection = revblk.GetBlockReferenceIds _
                   (False, False) For Each ref In refs Dim bref As BlockReference = Trans.GetObject(ref, OpenMode.ForRead) If bref.Name = blockname Then found = True Dim atts As AttributeCollection = bref.AttributeCollection For Each obj In atts Dim att As AttributeReference = Trans.GetObject _
                               (obj, OpenMode.ForRead, False) Select Case att.Tag Case revtag revVal = att.TextString Case datetag dateVal = att.TextString Case desc1tag desc1Val = att.TextString Case desc2tag desc2Val = att.TextString Case desc3tag desc3Val = att.TextString Case desc4tag desc4Val = att.TextString End Select Next End If Next End If Next End Using db.Dispose() Dim dwgpathend As Integer = pathtodwg.LastIndexOf("\") Dim dwgname As String = pathtodwg.Substring(dwgpathend + 1) Dim dwgnamelen As Integer = dwgname.Length - 4 dwgname = dwgname.Substring(0, dwgnamelen) Dim result(4) As String result(1) = dwgname result(2) = revVal result(3) = dateVal result(4) = desc1Val & " " & desc2Val & " " & desc3Val & " " & desc4Val Return result End Function

 I manually put in some line breaks so the code would fit, but this is he code I am having problems with, and it is breaking at the ReadDwgFile with the same eNotOpenForWrite.  This time the path is coming from a dialog, but I have tried both forcing the path and running a File.Exists to make sure I wasn't overlooking something obvious, and everything checked out fine.

 

There very well may be code problems further down the line but I can't get past the ReadDwgFile to work it out yet.

 

I must be missing something.

Thanks,

Jason

 

Message 7 of 10
JasonSelf
in reply to: JasonSelf

I found out what I was doing wrong.  CommandFlags.Session caused the problems.  Apparently I need to read up on CommandFlags.

 

Thanks,

Jason

Message 8 of 10
mchan01
in reply to: JasonSelf

Is there a way to open multiple dwg file in 1 editot?
I am using this code to read dwg file
Database db = doc.Database;
Database xdb = new Database(false, true);

xdb.ReadDwgFile(drawingpath, FileShare.ReadWrite, true, null);
objid = db.Insert(blockname, xdb, true);

on the 1st pass or the 1st dwg file it was working fine, i was able to display the dwg file. but after the 2nd, i loose control now of the 1st dwg file.
Message 9 of 10
DiningPhilosopher
in reply to: mchan01

You can open multiple drawing files in a loop, assigning each to the same variable, but you must call Dispose() on each Database before you assign it to another Database.

 

Don't use ReadDwgFile() to read multiple .DWG files with the same Database instance, just create a new instance for each file you want to read, and be sure to call Dispose() on each, or you will run into problems.

 

If you need to concurrently work with multiple DWG files that were opened via ReadDwgFile(), you will need to assign each of them to different variables and be sure to dispose of each.

 

The best way to ensure that an object is disposed is with the using() statement. You can find plenty of examples of its use with Database and ReadDwgFile() if you search here and on the net.

Message 10 of 10
mchan01
in reply to: DiningPhilosopher

I am trying to do saveAs from a file created from the most current version of autocad to 2010 version and i am getting a save error message of Warning: Error occured during save. We recommend that you run recover on the drawing.

here is my code
newDB.ReadDwgFile("C:\\MyDrawing\\Mass Haul-2_update2.dwg", FileOpenMode.OpenForReadAndAllShare, true, null);
newDB.SaveAs("C:\\MyDrawing\\Mass Haul-2_update22.dwg", DwgVersion.AC1024);

the new file would still be created though.

But if i open the original file and just do the saveas on the autocad menu , no errors encountered.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost