Script open read only autocad .dwg files

script open read only autocad file Here in this snippet we can open AutoCAD Read-only .dwg by script, first we will check in this code, whether the folder is Read-only by creating sub directory and then check autocad file .dwg IsReadOnly. Either case is Read-only then we will apply lisp and open Read-only, this method work for AutoCAD, ZWCAD, eCadViewer

Download

Example Source Snippets - cad.zip

namespace : using System.Runtime.InteropServices

How to open Read-only dwg file in script for Autocad , ZWCAD , eCadViewer ?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace Script_Plot
{
    public class cad
    {
        public static ZWCAD.IZcadApplication zcadApp = null;
        const string scriptPath = @"C:\DwgScriptToOpen.scr";


        public static bool CreateOpenDwgScriptFile(string dwgPath)
        {
            bool isScriptCreated = true;
            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(scriptPath)))//check dir exist
                    Directory.CreateDirectory(Path.GetDirectoryName(scriptPath));//create new dir

                string coma = @"""";
                StreamWriter sWriter = File.CreateText(scriptPath);
                sWriter.WriteLine("_sdi");
                sWriter.WriteLine("0");

                sWriter.WriteLine("filedia");
                sWriter.WriteLine("0");

                // read only check
                bool isReadonly = false;
                FileInfo fInfo = new FileInfo(dwgPath);

                string currDir = fInfo.DirectoryName;
                DirectoryInfo currDirInfo = new DirectoryInfo(currDir);

                try
                {
                    if (fInfo.IsReadOnly)//check dwg ready only
                    {
                        isReadonly = true;
                    }
                    else//check dir read only
                    {
                        currDirInfo.CreateSubdirectory("dummy_Checking");
                        Directory.Delete(currDir + "\\dummy_Checking");
                    }
                }
                catch (Exception)
                {
                    isReadonly = true;
                }

                if (isReadonly)
                {
                    sWriter.WriteLine("(vla-activate (vla-open (vla-get-documents (vlax-get-acad-object)) ");
                    sWriter.WriteLine(coma + dwgPath + coma + " :VLAX-TRUE))");
                }
                else
                {
                    sWriter.WriteLine("OPEN");
                    sWriter.WriteLine(coma + dwgPath + coma);
                }
                sWriter.Flush();
                sWriter.Close();
                sWriter.Dispose();
            }
            catch (Exception ex)
            {
                isScriptCreated = false;
                Console.WriteLine(ex.ToString());
            }

            return isScriptCreated;
        }       
    }
}

                 

smartsnipps.ecomparefiles.com © 2019, All Rights Reserved | Disclaimer: smartsnipps.eCompareFiles.com is free to use any code snippets without guarantee