.NET

Reply
Contributor
GeorgeKamieniecki
Posts: 15
Registered: ‎12-27-2011
Message 1 of 6 (327 Views)

Format problem with .net for datalinking Excel to AutoCad table

327 Views, 5 Replies
08-06-2012 01:12 PM

I am able to interactively create a table in AutoCad by defining a datalink to an Excel file. The table is creates with the varying row heights and varying column widths set up in Excel.

I have attempted to do the same using vb.net based on code examples from 'Through the Interface'.The Excel formatting is ignored and the resulting table width is very narrow and table height is huge.

Is there an option that I may have overlooked that forces the Excel formatting? I have set the table style to use Excel formatting without success.

 

Any help greatly appreciated

Distinguished Contributor
khoa.ho
Posts: 136
Registered: ‎09-15-2011
Message 2 of 6 (312 Views)

Re: Format problem with .net for datalinking Excel to AutoCad table

08-06-2012 02:29 PM in reply to: GeorgeKamieniecki

It will work if we use UpdateOption.UpdateColumnWidth and UpdateOption.UpdateRowWidth on update datalink. The code from "Through the Interface" (http://through-the-interface.typepad.com/through_the_interface/2007/08/creating-an-aut.html) can be rewritten as:

 

[CommandMethod("TFS")]
public static void TableFromSpreadsheet()
{
    // Hardcoding the string
    // Could also select for it
    const string dlName = "Import table from Excel demo";

    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    OpenFileDialog ofd =
        new OpenFileDialog(
            "Select Excel spreadsheet to link",
            null,
            "xls; xlsx",
            "ExcelFileToLink",
            OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles
            );

    DialogResult dr = ofd.ShowDialog();

    if (dr != System.Windows.Forms.DialogResult.OK)
        return;

    ed.WriteMessage("\nFile selected was \"{0}\".", ofd.Filename);

    PromptPointResult ppr = ed.GetPoint("\nEnter table insertion point: ");
    if (ppr.Status != PromptStatus.OK)
        return;

    // Remove the Data Link, if it exists already
    DataLinkManager dlm = db.DataLinkManager;

    ObjectId dlId = dlm.GetDataLink(dlName);

    if (dlId != ObjectId.Null)
    {
        dlm.RemoveDataLink(dlId);
    }

    // Create and add the Data Link
    DataLink dl = new DataLink
        {
            DataAdapterId = "AcExcel",
            Name = dlName,
            Description = "Excel fun with Through the Interface",
            ConnectionString = ofd.Filename,
            DataLinkOption = DataLinkOption.PersistCache
        };
    dl.UpdateOption |= (int)UpdateOption.AllowSourceUpdate;

    dlId = dlm.AddDataLink(dl);
    Transaction tr = doc.TransactionManager.StartTransaction();
    using (tr)
    {
        tr.AddNewlyCreatedDBObject(dl, true);
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

        Table tb = new Table { TableStyle = db.Tablestyle, Position = ppr.Value };
        tb.SetDataLink(0, 0, dlId, true);
        tb.UpdateDataLink(UpdateDirection.SourceToData, UpdateOption.UpdateColumnWidth | UpdateOption.UpdateRowHeight);
        tb.GenerateLayout();


        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        btr.AppendEntity(tb);
        tr.AddNewlyCreatedDBObject(tb, true);
        tr.Commit();
    }

    // Force a regen to display the table
    ed.Regen();
}

 

-Khoa

*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 3 of 6 (300 Views)

Re: Format problem with .net for datalinking Excel to AutoCad table

08-06-2012 02:41 PM in reply to: GeorgeKamieniecki

In Excel you may want to use:

// Acad 2010 //

oTable.SetColumnWidth(width);

oTable.SetRowHeight(height);

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
GeorgeKamieniecki
Posts: 15
Registered: ‎12-27-2011
Message 4 of 6 (282 Views)

Re: Format problem with .net for datalinking Excel to AutoCad table

08-07-2012 12:46 AM in reply to: GeorgeKamieniecki

Many thanks - that really did solve the problem.

Contributor
GeorgeKamieniecki
Posts: 15
Registered: ‎12-27-2011
Message 5 of 6 (282 Views)

Re: Format problem with .net for datalinking Excel to AutoCad table

08-07-2012 12:47 AM in reply to: Hallex

Thanks for your input - That works OK for uniform row height and column width.

Distinguished Contributor
khoa.ho
Posts: 136
Registered: ‎09-15-2011
Message 6 of 6 (260 Views)

Re: Format problem with .net for datalinking Excel to AutoCad table

08-07-2012 06:56 AM in reply to: GeorgeKamieniecki

You are welcome. I am happy that it works for you as it works on my side.

 

-Khoa

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community