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
Solved! Go to Solution.
Solved by SENL1362. Go to Solution.
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.
"How we think determines what we do, and what we do determines what we get."
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
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)
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
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];
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)));
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
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.