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

Casting an Autodesk Table to a class that extends it

1 REPLY 1
SOLVED
Reply
Message 1 of 2
Anonymous
431 Views, 1 Reply

Casting an Autodesk Table to a class that extends it

Sorry for the weird 

 

I'm writing a plug-in for AutoCAD 2014 using C# and the .NET platform. I extended Autodesk's Table class like so

 

public class OpeningDataTable : Autodesk.AutoCAD.DatabaseServices.Table

The idea is that I want to pull a table already drawn on an AutoCAD drawing out of the drawing as an instance of OpeningDataTable so I can manipulate the data with methods I've written. I am doing that like so

 

OpeningDataTable myTable = checkForExistingTable(true);

public Autodesk.AutoCAD.DatabaseServices.Table checkForExistingTable(bool isWindow)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Current drawing
            Transaction tr = doc.TransactionManager.StartTransaction();
            DocumentLock docLock = doc.LockDocument();
            TypedValue[] tableItem = new TypedValue[] { new TypedValue(0, "ACAD_TABLE") };
            SelectionFilter tableSelecFilter = new SelectionFilter(tableItem);
            Editor ed = doc.Editor; //Editor object to ask user where table goes, subclass of Document            using (tr)
            {
                PromptSelectionResult selectResult = ed.SelectAll(tableSelecFilter);
                if (selectResult.Status == PromptStatus.OK)
                {
                    SelectionSet tableSelSet = selectResult.Value;
                    for (int i = 0; i < tableSelSet.Count; i++)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Table tableToCheck = (Autodesk.AutoCAD.DatabaseServices.Table)tr.GetObject(tableSelSet[i].ObjectId, OpenMode.ForRead);
                        String tableTitle = tableToCheck.Cells[0, 0].Value.ToString();
                        if(tableTitle.Equals("Window Schedule") && isWindow == true)
                            return (OpeningDataTable)tableToCheck;
                        if (tableTitle.Equals("Door Schedule") && isWindow == false)
                            return (OpeningDataTable)tableToCheck;
                    }
                }
                return null;
            }
        }

However I get an error saying that I cannot convert a Table object (the parent class) to an OpeningDataTable object (the child class). I'm looking for a simple workaround to this issue

1 REPLY 1
Message 2 of 2
_gile
in reply to: Anonymous

Hi,

 

Your checkForExistingTable() method returns an Autodesk.AutoCAD.DatabaseServices.Table instance, not an OpeningDataTable one.

If you want to implicitly (or explicitly) cast a Table instance into an OpeningDataTable one, you have to implement (implicit or explicit) conversion operators in the OpeningDataTable class definition.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

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