how update dwg files values linkend to excel table.

how update dwg files values linkend to excel table.

Anonymous
Not applicable
611 Views
7 Replies
Message 1 of 8

how update dwg files values linkend to excel table.

Anonymous
Not applicable

Dear all,

I have several dwg files linked to an excel table.

When I update some values into the exel I need to open each dwg file, than save and close in order to change the values linked  (foreach dwg file the commands are: open + _DATALINKUPDATE _U _K +save +close).

 

I wonder if there is a way throught .net (C#) in order to handle the whole dwg list once

 

Any suggestion will be appreciated

cheers

M.

 

 

0 Likes
612 Views
7 Replies
Replies (7)
Message 2 of 8

norman.yuan
Mentor
Mentor

Since you are asking in AutoCAD .NET API forum, I assume you are to run some code in AutoCAD to batch updating drawings for the DataLinks in them. Yes, it can be done rather easily. Something like:

 

1. Obtain a drawing file list; Then for each drawing;

2. Open the drawing file as side database;

3. Call atabase.DataLinkManager.Update() to update all DataLinks; or

4. Open DataLinkDictionary (by database.DataLinkDictionaryId), and loop through DataLinks and call DataLink.Update() as needed;

5. Call Database.SaveAs() to save changes made to the side database.

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

Hi Norman 

thank you for your attention. You are right, currently I'm using Autocad commands but my wish is update the links throught .net, the program could work in Autocad or (better) stand alone.

I had some programming experiences for other issues (solved thanks to the forum) but I'm not so skilled and  I wonder if is possible to have a draft of the method I should use,  could it looks like the follow?

Cheers

M.

 

            try
            {
                LogMessageToFile("\n##### START PROCESS #####\n");
                string[] dwgFiles = Directory.GetFiles(pathDwgFiles, "*.dwg", SearchOption.AllDirectories);
                foreach (var dwgfile in dwgFiles)
                {
                    LogMessageToFile("\nProcessing: " + Path.GetFileName(dwgfile));

                    using (var db = OpenSideDatabase(dwgfile))
                    {
                        HostApplicationServices.WorkingDatabase = db;
                        db.RetainOriginalThumbnailBitmap = true;
                        db.Database.DatalinkManager.Update();
                        db.SaveAs(dwgfile, true, DwgVersion.Current, null);
                    }  
                }
            }
            catch (System.Exception ex)
            {
                CadApp.ShowAlertDialog("Error:\r\n" + ex.Message);
            }
            finally
            {
                LogMessageToFile("\n##### END PROCESS #####");
                HostApplicationServices.WorkingDatabase = workingDb;
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }

 

 

 

 

0 Likes
Message 4 of 8

norman.yuan
Mentor
Mentor

I think your code logic is right (I am sure you know DataLinkManager.Update() has 2 arguments to be supplied: UpdateDirection and UpdateOption).

 

Additional things:

 

1. You may want to limit how many drawings to be processed in an AutoCAD session, if you have hundreds or even thousands files to update, because of inevitable memory fragment due to repeatedly opening/closing drawing files;

2. You may want to show some kind of visual progress indicator, such as progress bar UI, so that user knows AutoCAD is doing something as expected.

 

You cannot do it directly from stand-alone EXE, because there is AutoCAD COM API to access DataLink.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 8

micle.space
Enthusiast
Enthusiast

Dear all, I would like to go ahaed with this method so this is my code:

 

//current session
var openedDb = HostApplicationServices.WorkingDatabase;
try
{
	string[] dwgFiles = Directory.GetFiles(PathDwgFiles, "*.dwg", SearchOption.AllDirectories);
	foreach (var dwgfile in dwgFiles)
	{	
		var db = new Database(false, true);
		db.ReadDwgFile(dwgFileName, FileOpenMode.OpenForReadAndWriteNoShare, false, null);
		using (db)
		{
			HostApplicationServices.WorkingDatabase = db;
			db.RetainOriginalThumbnailBitmap = true;
			
			db.DataLinkManager.Update(UpdateDirection.SourceToData, UpdateOption.None);

			db.SaveAs(dwgfile,true,DwgVersion.Current,null);

			//back to current session
			HostApplicationServices.WorkingDatabase = openedDb;
		}

	}
}

 

the results are quite odd...

I have always the same base-drawing file1.dwg, file2.dwg.... all linked to a common excel file but to different cells. During the process something strange happen: Autocad sorts a message like: "impossible save the file into the speficied format..." and when I open that file all drawing is missing. Follow a image (blue square all is ok, red square drawing is missing)

 

HappyEnding.pngmore and more weird if I test again the same 4 files what was ok and at the first stroke is not ok at the second. Example I test 4 files:

First test File 1, 2 and 4 ok, but 4 wasn't.

Second test File 1 and 3 ok, but 2 and 4 wasn't.

What happen?

Thanks in advance

0 Likes
Message 6 of 8

norman.yuan
Mentor
Mentor

While this thread is about DataLink, it seems to me that your issue has nothing to do with calling DataLinkManager.Update(). 

 

Do you use plain AutoCAD, or AutoCAD vertical (Arch/MEP/Mech/Plant3D... especially the one used to create those drawings)? Version?

 

You may want to do some test to narrow the scope of possible reason(s).

1. You can comment out db.DataLinkManager.Update() to confirm if the issue is related to DataLink updating.

2. You can open multiple drawings in AutoCAD, and loop though the opened drawing to update DataLinks and save the change. Verify if the issue occurs or not.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 8

micle.space
Enthusiast
Enthusiast

Hi Norman thank for your comment.

The main wpf window has a progressbar, just indeterminate.

The visibility is visible before the UpadateProcess and then Hidden when the process is ended.

The process is in an await Task, this is the code:

 

            MyUtilsCad myUcad = new MyUtilsCad();
            //some settings of my utility class
            myUcad.PathDwgFiles = Path.....;
            
            //progressbar pbProcess visibility and task run for 
            //updateExcellLinks
            pbProcess.Visibility = Visibility.Visible;
            await Task.Run(() => myUcad.UpdateExcelLinks());
            pbProcess.Visibility = Visibility.Hidden;

 

today I will try to remove the task, I think this odd behaviour could be due to this.

If I solve, at least I will open a new 3d in order to understand how manage the tasks with autocad process.

0 Likes
Message 8 of 8

micle.space
Enthusiast
Enthusiast

yes, the problem is the thread.

0 Likes