Bitness agnostic ReadDWG

The following source code sample demonstrate how to load DWG/DXF files from a .NET application compiled against AnyCPU. It includes the operating system bitness detection, dynamic loading of the Eyeshot x86 or x64 DLL from a separate folder and the instantiation of the ReadAutodesk class using reflection.

 

Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    reuben alfred

    namespace devDept.Eyeshot.Reader
    {
    using Entities;
    using Geometry;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;


    public class RuntimeAutodeskReader
    {
    private object Reader { get; set; }
    public Assembly Assembly { get; private set; }
    public string AppPath { get; private set; }
    public string FileToRead { get; set; }
    public string AssemblyPathAndName { get; private set; }
    public string Arch { get; private set; }
    public string AssemblyName { get; } = "devDept.Eyeshot.Control";
    public string AssemblyVersion { get; } = "v9";
    public List<Type> Types { get; private set; }


    private MethodInfo Process { get; set; }
    private MethodInfo AddToScene { get; set; }
    private MethodInfo Unlock { get; set; }
    private Type ReadAutodesk { get; set; }


    public Point3D Min { get { return getMinPropValue(); } }


    public Point3D Max { get { return getMaxPropValue(); } }


    public Entity[] Entities { get { return getEntitiesPropValue(); } }


    private PropertyInfo entities;
    private PropertyInfo min;
    private PropertyInfo max;
    public string SerialNumber { get; set; }



    public RuntimeAutodeskReader(string serialNumber, string fileToRead)
    {
    if (!File.Exists(fileToRead))
    {
    throw new FileNotFoundException(fileToRead);
    }

    if (string.IsNullOrEmpty(serialNumber))
    {
    throw new ArgumentException("Serial number to unlock is null or empty");
    }


    SerialNumber = serialNumber;
    FileToRead = fileToRead;
    AppPath = Path.GetDirectoryName(new System.Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
    Arch = (Environment.Is64BitProcess) ? "x64" : "x86";
    AssemblyPathAndName = string.Format("{0}\\{1}\\{2}.{1}.{3}.dll", AppPath, Arch, AssemblyName, AssemblyVersion);


    if (!File.Exists(AssemblyPathAndName))
    {
    throw new FileNotFoundException(string.Format("Cannot find the assembly ({0})", AssemblyPathAndName));
    }


    Assembly = Assembly.LoadFrom(AssemblyPathAndName);
    Types = new List<Type>(Assembly.GetExportedTypes());


    ReadAutodesk = Types.First(t => t.Name.Equals("ReadAutodesk", StringComparison.OrdinalIgnoreCase));


    if (ReadAutodesk == null)
    {
    throw new Exception(string.Format("Autodesk reader is not found in the assembly ({0})", AssemblyName));
    }


    construct();
    }


    public void addToScene(ViewportLayout layout, Solid solid)
    {
    layout.Entities.Add(solid);
    }


    private Point3D getMaxPropValue()
    {
    MethodInfo[] accessors = max.GetAccessors();


    MethodInfo mi = getGetter(accessors);


    return (Point3D)mi.Invoke(Reader, new object[] { });
    }
    private Entity[] getEntitiesPropValue()
    {
    MethodInfo[] accessors = entities.GetAccessors();
    MethodInfo mi = getEntitiesGetter(accessors);


    return (Entity[])mi.Invoke(Reader, new object[] { });
    }


    private MethodInfo getEntitiesGetter(MethodInfo[] accessors)
    {
    foreach (MethodInfo accessor in accessors)
    {
    if (accessor.ReturnType == typeof(Entity[]))
    {
    return accessor;
    }
    }


    return null;
    }


    private MethodInfo getGetter(MethodInfo[] accessors)
    {
    foreach (MethodInfo accessor in accessors)
    {
    if (accessor.ReturnType == typeof(Point3D))
    {
    return accessor;
    }
    }


    return null;
    }


    private Point3D getMinPropValue()
    {
    MethodInfo[] accessors = min.GetAccessors();


    MethodInfo mi = getGetter(accessors);


    return (Point3D)mi.Invoke(Reader, new object[] { });
    }
    private void construct()
    {
    ConstructorInfo constructor = ReadAutodesk.GetConstructor(new Type[] { typeof(string), typeof(string), typeof(bool), typeof(bool), typeof(bool) });


    if (constructor != null)
    {
    Reader = constructor.Invoke(new object[] { FileToRead, null, false, false, false });


    Unlock = ReadAutodesk.GetMethod("Unlock", new Type[] { typeof(string) });
    unlock();
    Process = ReadAutodesk.GetMethod("DoWork", new Type[] { });
    AddToScene = ReadAutodesk.GetMethod("AddToScene", new Type[] { typeof(ViewportLayout), typeof(int) });


    min = ReadAutodesk.GetProperty("Min");
    max = ReadAutodesk.GetProperty("Max");
    entities = ReadAutodesk.GetProperty("Entities");


    if (Reader == null)
    {
    throw new Exception(string.Format("Cannot read the file ({0}).", FileToRead));
    }


    if (Unlock == null)
    {
    throw new Exception(string.Format("Cannot get Unlock method."));
    }


    if (Process == null)
    {
    throw new Exception(string.Format("Cannot get DoWork method"));
    }


    if (AddToScene == null)
    {
    throw new Exception(string.Format("Cannot get AddToScene method"));
    }


    if (min == null)
    {
    throw new Exception(string.Format("Cannot get Min property"));
    }


    if (max == null)
    {
    throw new Exception(string.Format("Cannot get Max property"));
    }


    if (entities == null)
    {
    throw new Exception(string.Format("Cannot get Entities property"));
    }
    }
    else
    {
    throw new RuntimeAutodeskReaderException("Cannot get ReadAutodesk class!");
    }
    }


    public void process()
    {
    Process.Invoke(Reader, new object[] { });
    }
    public void addToScene(ViewportLayout layout)
    {
    AddToScene.Invoke(Reader, new object[] { layout });
    }
    public void unlock()
    {
    Unlock.Invoke(Reader, new object[] { SerialNumber });
    }
    public override string ToString()
    {
    return string.Format("AppPath={0} \nFileToRead-{1} \nAssemblyPathAndName={2} \nArch{3} \nAssemblyName={4} \nTypes={5}",
    AppPath,
    FileToRead,
    AssemblyPathAndName,
    Arch,
    AssemblyName,
    getTypesWithNewLines());
    }


    private string getTypesWithNewLines()
    {
    string result = "";


    foreach (Type type in Types)
    {
    result += string.Format("{0}\n", type.Name);
    }


    return result;
    }
    }


    public class RuntimeAutodeskReaderException : Exception
    {
    public RuntimeAutodeskReaderException(string message) : base(message)
    {


    }
    }
    }

    Edited by reuben alfred
Please sign in to leave a comment.