How to get an instance of BricsCAD18 by calling Activator.CreateInstance from C#.NET?

We move from BricsCAD16 to 18. Our C#.NET-Application calls BricsCAD16 by Activator.CreateInstance.
internal static object CreateInstanceFromProgId16(string progId) { try { Type objectType = Type.GetTypeFromProgID(progId); return Activator.CreateInstance(objectType, true); } catch (ArgumentNullException) { throw new FileNotFoundException("Missing BricsCAD-Executable"); } }
Using BricsCAD18 the process is started but only the control to select the view is shown. The only workaround I got is using Process.Start and fishing for the instance. Rather a hack than a solution...

internal static object CreateInstanceFromProgId18(string progId) { RegistryKey bricsCadProgId = Registry.ClassesRoot.OpenSubKey(progId + "\\CLSID"); RegistryKey bricsCadLocalServer = Registry.ClassesRoot.OpenSubKey("CLSID\\" + bricsCadProgId.GetValue("") + "\\LocalServer32"); string executablePath = bricsCadLocalServer.GetValue("").ToString().Replace("/Automation", String.Empty).Trim(); var bricsCadProcess = new Process { StartInfo = new ProcessStartInfo { FileName = executablePath } }; bricsCadProcess.Start(); object bricsCadInstance = null; for (int i = 0; i < 1000; i++) { try { bricsCadInstance = GetInstanceFromProgId(progId); break; } catch {} Thread.Sleep(30); } return bricsCadInstance; }
I suppose this is caused by the /Automation Parameter set in the windows registry. Is there a possibility to use the nice old Activator-way?

Comments

  • Better check with the developers with a support ticket: https://www.bricsys.com/en-eu/support/#51

  • abeltello
    edited June 2018

    here is an example

    public static void ConnectToAcad()
    {
    
        AcadApplication acAppComObj = null;
       //The PROGID for BRICSCAD
        const string strProgId = "BricscadApp.AcadApplication";
    
        // Get a running instance of BRICSCAD
        try
        {
            acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
        }
        catch // An error occurs if no instance is running
        {
            try
            {
              // Create a new instance of BRICSCAD
              acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
            }
            catch
            {
                // If an instance of AutoCAD is not created then message and exit
                System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
                                                   " could not be created.");
                return;
            }
    
            // Display the application and return the name and version
            acAppComObj.Visible = true;
            System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name + 
                                               " version " + acAppComObj.Version);
    
            // Get the active document
            AcadDocument acDocComObj;
            acDocComObj = acAppComObj.ActiveDocument;
        }
     }
    
This discussion has been closed.