ReadDwgFile give eRepeatedDwgRead error on newly created drawing

ReadDwgFile give eRepeatedDwgRead error on newly created drawing

genelibardiDCZT4
Contributor Contributor
521 Views
6 Replies
Message 1 of 7

ReadDwgFile give eRepeatedDwgRead error on newly created drawing

genelibardiDCZT4
Contributor
Contributor

I'm trying read a template file (.dwt) into a freshly created drawing. The ReadDwgFile files with eRepeatedDwgRead even though nothing has ever been read into this db before. How can I get this file read into my DB?

 

public static void LoadTemplate()
{
    Logging.AddLog("Loading kb Drawing Template.");

    string baseTemplateUrl = ""; //redacted for forum;

    // Get the temp file name for the base template
    string baseLocalFilePath = Path.Combine(Path.GetTempPath(), GetURLBasePath(baseTemplateUrl) + ".dwg");

    try
    {
        using (WebClient client = new WebClient())
        {
            // Download the base template file from the URL to the local file path
            try
            {
                client.DownloadFile(baseTemplateUrl, baseLocalFilePath);
            }
            catch (System.Exception e)
            {
                Logging.AddLog($"Could not download the kB base template url:\n{e}", "error");
                return;
            }
            // Read the base template into the dwg db
            try
            {
                KbCadHostAcad.Database.ReadDwgFile(baseLocalFilePath, FileOpenMode.OpenForReadAndReadShare, false, "");
            }
            catch (System.Exception e)
            {
                Logging.AddLog($"Failed to read kB Template file into the drawing database:\n {e}", "error");
                return;
            }
        }
        return;
    }
    catch (System.Exception e)
    {
        Logging.AddLog($"Error loading kb base template.", "error");
        return;
    }
}

 

0 Likes
Accepted solutions (1)
522 Views
6 Replies
Replies (6)
Message 2 of 7

ActivistInvestor
Mentor
Mentor

You are showing code that obtains a Database from a property of an object which you do not show. No one can tell you what the problem is without seeing all of the relevant code.

 

You need to show the code that creates the Database that's used in your code. 

 

0 Likes
Message 3 of 7

genelibardiDCZT4
Contributor
Contributor

Thank you for your reply. This is a very large add-in project with a lot of code. It would be very difficult to send it all to you but I will try to include the relevant things. 


For context, it is an add-in with a ribbon UI that is loaded automatically from a bundle. The end user starts AutoCAD (2024 in this case) and starts a new drawing in the normal way. Then they click a button on the add-in ribbon panel which contacts an external web service and gets a data set that includes instructions for building a model and drawing layouts.


The database is just the one that is created when the new drawing is created and is accessed like this:

public static Document Document
{
    get
    {
        return Application.DocumentManager.MdiActiveDocument;
    }
}

public static Database Database
{
    get
    {
        return Document.Database;
    }
}

 

The LoadTemplate() method is executed from an "onDocOpen" method that is bound to the Application.DocumentManager.DocumentCreated event and is the first thing done there.

 

I hope this helps clarify things.

0 Likes
Message 4 of 7

Norman_Yuan
Mentor
Mentor

You DO NOT use the MdiActiveDocument.Database to read DWG file. Since you download a drawing file and save it to the local folder, you simply call Application.DocumentManager.Add[Open]() extension method to open the template/drawing.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 7

genelibardiDCZT4
Contributor
Contributor

Thank you for your reply, Mr. Yuan.

 

I am not downloading a dwg file that I want to open. I am downloading a template file (dwt) and I want to include everything in that file in the currently open dwg.

0 Likes
Message 6 of 7

Norman_Yuan
Mentor
Mentor
Accepted solution

Are you saying that have a current drawing that is opened in AutoCAD as MdiActiveDocument and you want to: 1). keep its EXISTING contents; 2. get all contents from the download drawing file (be it template DWT file, or DWG file, it really does not matter) into the MdiActiveDocument? In the case of keeping existing contents and adding contents from other drawing file, you need to open the downloaded file as SIDE DATABASE, and then use WBlockCloneObjects() method to copy the contents into the existing opened drawing, where you would specify how to handle duplicated content (overwrite, or ignore...). You would call it many times targeting different object owners (Symbol tables, spaces...). 

 

However, I am wondering why you want to do that (bring all contents from a drawing to an existing drawing). You may want to explain a bit more. However, you cannot use ReadDwgFile() with the database of an document opened in AutoCAD, that is the reason you got the said error. 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

genelibardiDCZT4
Contributor
Contributor
Thank you very much for your response. This is very helpful information.
Long story as to why I'm trying to do this, but in light of this
information, I will most likely try to find another approach. Thanks again.
0 Likes