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

How to get the Autocad profile list?

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
wrenchinternal
2115 Views, 10 Replies

How to get the Autocad profile list?

Hi

 

I am trying to retrieve all the profiles that i have created in my autocad Application.

I was trying to do this using reflection .

But i am only able to get the active profile.

Following is my c# code

 

Type oClassType;
object[] oParams;

object oAcadApp = null;

if (oAcadApp == null) //Is AutoCAD already connected?
{
oClassType = Type.GetTypeFromProgID("AutoCAD.Application.19");
oAcadApp = Activator.CreateInstance(oClassType);
oParams = new Object[1];
oParams[0] = false;

oAcadApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty,
null, oAcadApp, oParams);
}

 

object oAcadPreferences = oAcadApp.GetType().InvokeMember("Preferences", BindingFlags.GetProperty, null, oAcadApp, null);
object oAcadProfiles = oAcadPreferences .GetType().InvokeMember("Profiles", BindingFlags.GetProperty, null, oAcadPreferences , null);

 

 

 

I would like to get a solution for this as early as possible.

Thanks in Advance

Manosh

 

10 REPLIES 10
Message 2 of 11
dgorsman
in reply to: wrenchinternal

You may end up digging into the registry for it, but it *might* be possible to scrape the names from AWS files in the AutoCAD support path.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 3 of 11
BlackBox_
in reply to: wrenchinternal

Given the speed of querying registry vs. external file like XML, I should think it quite simple to iterate profiles only needing product key from main application, instead of COM.

My $0.02


"How we think determines what we do, and what we do determines what we get."

Message 4 of 11
wrenchinternal
in reply to: dgorsman

hi

 

I didnt understand fully about what you said.How do i read the profiles from registry and AWS file?

I have attached a picture of the exact detail i want to fetch from autocad.

 

 

regards,

Manosh

 

Message 5 of 11
SENL1362
in reply to: wrenchinternal

This might help you to solve the issue

 

using Autodesk.AutoCAD.Interop;

//AddReference>Com>AutoCAD 2012 Type Library
//Acad2014: Autodesk.AutoCAD.Interop.dll

 

AcadPreferences  acadPreferences=(AcadPreferences)Application.Preferences;

 

AcadPreferencesProfiles acadProfiles = acadPreferences.Profiles;

object profNames=null;
acadProfiles.GetAllProfileNames(out profNames);

 

Other usefull Profile Methodes:

string activeProfile = acadProfiles.ActiveProfile;

 

acadProfiles.ExportProfile(string ProfileName, string regFile)
acadProfiles.ImportProfile(string ProfileName, string regFile, bool includePathInfo)
acadProfiles.ResetProfile(string profile)
acadProfiles.RenameProfile(string origProfileName, string newProfileName)
acadProfiles.CopyProfile(string oldProfileName, string newProfileName)

 

 

Message 6 of 11
wrenchinternal
in reply to: SENL1362

Hi

 

I have tried this way,but i need to do it using reflection in c# as we cannot do it version dependent.

So i have been trying to do it via reflection in C#.

When i try to invoke the method GetAllProfileNames it is returning null

Can you help me on that

 

Message 7 of 11
SENL1362
in reply to: wrenchinternal

 

The Profiles methode return the ProfileNames via an OUT argument, so you have to Invoke that methode with arguments.

in this case an argument returning values (out or ref)

Google a bit and found this.

 

create an object array and reserve a space for one returning argument (null)

then invoke the member with the arguments

Haven't tested this code but give it a try

 

object[] arguments = new object{null};
object oAcadProfiles = oAcadPreferences .GetType().InvokeMember("Profiles", BindingFlags.GetProperty, null, oAcadPreferences , arguments );
string[] profileNames=arguments[0];

 

Message 8 of 11
SENL1362
in reply to: SENL1362

 

The previous anwer retrieved from google appears to be incorrect.
Sorry about that.
 

Fortunately i found two solutions for you:

1. the simple solution using Dynamics;

2. the invoke solution with some help from TonyT.

 


//Start with a modied version of youre code

Type acadType = Type.GetTypeFromProgID("AutoCAD.Application.19.1");

var acad = Activator.CreateInstance(acadType);


//this is what we want to get returned

string[] acadProfileNames = null;

 


//Using Dynamics

dynamic acadPreferences = ((dynamic)acad).Preferences;

dynamic acadProfiles = acadPreferences.Profiles;

acadProfiles.GetAllProfileNames(out acadProfileNames);

if (acadProfileNames == null)
   throw new System.Exception("Error GetAllProfileNames: Failed to return List of AutoCAD Profile Names");

Console.WriteLine("AutoCAD Profile Names: {0}", (string.Join(": ", acadProfileNames)));

 

//using Invoke

//Slightly modified shortcuts  from TonyT

public static class LateBinding

{

public static object Get(this object obj, string propName, params object[] parameter)

{

return obj.GetType().InvokeMember(propName, BindingFlags.GetProperty, null, obj, parameter);

}

public static void Set(this object obj, string propName, params object[] parameter)

{

obj.GetType().InvokeMember(propName, BindingFlags.SetProperty, null, obj, parameter);

}

public static object Invoke(this object obj, string methName, object[] args, ParameterModifier[] mods)

{

return obj.GetType().InvokeMember(methName, BindingFlags.InvokeMethod, null, obj, args, mods, null, null);

}
}

 

//Samples using TonyT latebindings

//acad.Get("Preferences").Get("Display").Set("CursorSize", 10);

//object acadProfPref = acad.Get("Preferences").Get("Profiles");

 


//Reserve space for the returning argument, placeholder==NULL

object[] getAllProfilesArgs = new Object[] { null };

//First argument as reference(out)

ParameterModifier paramMods = new ParameterModifier(1);

paramMods[0] = true;
ParameterModifier[] argModifiers = { paramMods };
// all modifiers as a single object

acad.Get("Preferences").Get("Profiles").Invoke("GetAllProfileNames", getAllProfilesArgs,argModifiers);

if (getAllProfilesArgs[0] == null)
  throw new System.Exception("Error GetAllProfileNames: Failed to return List of AutoCAD Profile Names using Reflection");

acadProfileNames = (string[])getAllProfilesArgs[0];

Console.WriteLine("AutoCAD Profile Names: {0}", (string.Join(": ", acadProfileNames)));

 

 

Message 9 of 11
wrenchinternal
in reply to: SENL1362

Hi

 

Thank you so much for your reply.I tried it using dynamics and it worked.

I also have one more scenario to handle and that is with exported profile path.

 

If i have an exported profiles say architecture.ARG file with me.

i want to load my application or a drawing in this exported profile type.

How would i do this throught my c#.net application?Should i first import this profile?

or can i open the application along with the profile file path?

like eg:

  ProcessStartInfo psi = new ProcessStartInfo( @"C:\Program Files\Autodesk\AutoCAD 2012 - English\acad.exe", "/p C:\\Users\\indu\\New folder\\architecture.arg");

 

this didnt work for me.

 

Regards

Message 10 of 11
SENL1362
in reply to: wrenchinternal

Have you tried it with a regular DOS cmd?

 

Message 11 of 11
wrenchinternal
in reply to: SENL1362

Thank you.

 

I solved it by first importing the .arg file and then assigning that profile if required.I think that the only way to do this.

Thank you for your help.

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