TheSwamp

Code Red => .NET => Topic started by: gile on June 03, 2009, 02:01:36 AM

Title: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 02:01:36 AM
Hi,

I need to get the support search pathes.
I read it can be done using COM, so I try this :

AcadPreferences prefs = (AcadPreferences)Application.Preferences;

It works fine with 2007, 2008 and 2009 versions but returns an exception with A2010:

"Impossible d'effectuer un cast d'un objet COM de type 'System.__ComObject' en
type d'interface 'Autodesk.AutoCAD.Interop.AcadPreferences'.
"

"Unable to make a cast of a COM object of type 'System.__ComObject' in
interface type 'Autodesk.AutoCAD.Interop.AcadPreferences."

I know I can have it also reading the registry, but I wanted to know if this problem is knowed and if ther're solutions.
Thanks.
Title: Re: cast error with COM Objet and A2010
Post by: It's Alive! on June 03, 2009, 02:34:36 AM
an alternative method might be to p/invoke acedGetEnv, I think there are a few examples around
Title: Re: cast error with COM Objet and A2010
Post by: It's Alive! on June 03, 2009, 02:38:00 AM
there is also a similar thread , http://www.theswamp.org/index.php?topic=20237.0
Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 02:51:13 AM
Thanks Daniel

p/invoke methods are still a little over my head, but I'll try.
Title: Re: cast error with COM Objet and A2010
Post by: It's Alive! on June 03, 2009, 02:55:29 AM
did you see this entry by Master Kerry?

http://www.theswamp.org/index.php?topic=20237.msg246496#msg246496
Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 03:02:11 AM
Yes, it seems to be a nice way too.
Thanks again.
Title: Re: cast error with COM Objet and A2010
Post by: Kerry on June 03, 2009, 03:18:24 AM
What is this Master stuff Daniel ...  :-D

I'm just an old Lisp hacker trying to keep up.

///kdub

ps: but thanks for the thought :)
Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 07:22:45 AM
Another question to the 'masters'

Have I to dispose the IConfigurationSection object got with OpenCurrentProfile() ?

Here's how I wrote the method, is it the good way ?

Code: [Select]
private void helpButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (IConfigurationSection profile = AcApp.UserConfigurationManager.OpenCurrentProfile())
                {
                    string acadProp =(string)profile.OpenSubsection("General").ReadProperty("ACAD", string.Empty);
                    string[] pathes = acadProp.Split(new char[] { ';' });
                    string fullName = "";
                    for (int i = 0; fullName == "" && i != pathes.Length; i++)
                    {
                        string path = pathes[i];
                        if (path != "")
                        {
                            string[] files = Directory.GetFiles(path, "cadnormh.chm");
                            if (files.Length > 0)
                                fullName = files[0];
                        }
                    }
                    if (fullName != "")
                        Help.ShowHelp(this, fullName);
                    else
                        ed.WriteMessage("Le fichier \"CADnormH.chm\" est introuvable.\n");
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }
Title: Re: cast error with COM Objet and A2010
Post by: It's Alive! on June 03, 2009, 08:59:18 AM
personally, I don't think you need to call dispose since IConfigurationSection is just an interface class..
but if you want to be on the safe side, do it that way you have it.
just a note, IMO you should initialize empty strings with string.empty and use string.IsNullOrEmpty() to test against null or empty strings  :-)
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 10:24:55 AM
I need to get the support search pathes.

Hi Gile,

If you just want to extract the paths, you can use ACADPREFIX system variable - maybe.
Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 10:38:04 AM
Thanks again Daniel
I thaught I had to dispose because the interface is disposable.

LE,
You're the man !
Sometimes, I can't see my nose in the middle of my face :oops:
Thanks !
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 11:22:18 AM
LE,
You're the man !
Sometimes, I can't see my nose in the middle of my face :oops:
Thanks !

:)

Merci, tu es le bienvenu
Title: Re: cast error with COM Objet and A2010
Post by: Glenn R on June 03, 2009, 11:34:34 AM
From memory, this will not pick up any changes made during the current session, as the registry is written on AutoCAD shutdown...at least that's what I recall anyway...
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 12:36:52 PM
Hi Gile,

I normally find my file depencies, based on the location of my application, maybe if you use something like:

Code: [Select]
public string GetAppPath()
{
    System.Reflection.Module[] modules = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
    string aPath = System.IO.Path.GetDirectoryName(modules[0].FullyQualifiedName);
    if ((aPath != "") && (aPath[aPath.Length - 1] != '\\'))
    {
        aPath += '\\';
    }
    return aPath;
}

for example:

Command: APPATH
C:\Programming\C#\BlockManager\bin\Debug\

string file = "cadnormh.chm";
string lookFor = appPath + file;

Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 01:10:53 PM
Muchas gracias Luis

Most of the time the help file should be in the same folder as the dll, but if the user has moved it to another folder in the search path, I'd rather use something like the findfile LISP function.

Your snippet will be usefull for a newby like me.
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 02:25:02 PM
I'd rather use something like the findfile LISP function.

then, let see if this works... not fully tested (on A2009) - maybe :)

Code: [Select]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedFindFile")]
static extern int FindFile(StringBuilder fname, StringBuilder result);

[CommandMethod("TestFind")]
public void testfind()
{
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    StringBuilder sResult = new StringBuilder(128);
    StringBuilder sSearch = new StringBuilder("acad.exe");
    FindFile(sSearch, sResult);
    ed.WriteMessage("{0}", sResult);
}
Title: Re: cast error with COM Objet and A2010
Post by: T.Willey on June 03, 2009, 02:46:36 PM
.Net has a findfile, no need to import from a dll.  FYI...

HostApplicationServices.Current.FindFile
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 02:49:42 PM
.Net has a findfile, no need to import from a dll.  FYI...

HostApplicationServices.Current.FindFile

I saw that... and did not tested... :)

Thanks, Tim.
Title: Re: cast error with COM Objet and A2010
Post by: T.Willey on June 03, 2009, 02:50:19 PM
.Net has a findfile, no need to import from a dll.  FYI...

HostApplicationServices.Current.FindFile

I saw that... and did not tested... :)

Thanks, Tim.

I use it in one of my codes.  You're welcome Luis.

Edit:  Added said code.

Code: [Select]
public static string WillLoad (string FilePath, Database db) {
    string FoundAt = "";
    string[] tempStrAr = FilePath.Split('.');
    string FileExt = tempStrAr[tempStrAr.Length - 1];
[color=red]    try { FoundAt = HostApplicationServices.Current.FindFile(FilePath, db, FindFileHint.Default); }[/color]
    catch {}
    if ( !string.Compare(FoundAt, string.Empty).Equals( 0 ) )
            return FoundAt;
    if (string.Compare(FilePath.Substring(0, 2), "..").Equals(0) || string.Compare(FilePath.Substring(0, 1), ".").Equals(0)) {
        string[] XrPathArray = FilePath.Split('\\');
        string PartialPath = "";
        for (int i = 1; i < XrPathArray.Length; ++i) {
            PartialPath = PartialPath + "\\" + XrPathArray[i];
        }
        FileInfo DwgInfo = new FileInfo(db.Filename);
        string tempFilePath = DwgInfo.DirectoryName + PartialPath;
        try { FoundAt = HostApplicationServices.Current.FindFile(tempFilePath, db, FindFileHint.Default); }
        catch {}
        if ( !string.Compare(FoundAt, string.Empty).Equals( 0 ) )
                return FoundAt;
    }
    return string.Empty;
}
Title: Re: cast error with COM Objet and A2010
Post by: Spike Wilbury on June 03, 2009, 02:52:21 PM
.Net has a findfile, no need to import from a dll.  FYI...

HostApplicationServices.Current.FindFile

I saw that... and did not tested... :)

Thanks, Tim.

I use it in one of my codes.  You're welcome Luis.

10-4

been out of the c# coding for a while until early today, that I played a little bit :)
Title: Re: cast error with COM Objet and A2010
Post by: gile on June 03, 2009, 03:15:49 PM
Thank you both  :-o

But all this is quite over my head, the only attributes I use at the moment are CommandMethod and LispFunction.
Today, I'm proud I've been able to get the support files search path folders (using COM, Registry reading, ConfigurationManager or DWGPREFIX sysvar) and iterate through them to search a file.

I'll try to study all this, but it's so far from my little LISP knowledge.
Title: Re: cast error with COM Objet and A2010
Post by: xsfhlzh on June 04, 2009, 06:31:23 AM
http://www.mccad.net/bbs/dispbbs.asp?boardID=33&ID=75736&page=2
Title: Re: cast error with COM Objet and A2010
Post by: Kerry on June 04, 2009, 06:49:16 AM


Thanks :)

See if this works
http://www.microsofttranslator.com/BV.aspx?ref=IE8Activity&a=http%3a%2f%2fwww.mccad.net%2fbbs%2fdispbbs.asp%3fboardID%3d33%26ID%3d75736%26page%3d2