.NET

Reply
Valued Contributor
C_Witt
Posts: 55
Registered: ‎03-22-2010
Message 1 of 11 (2,000 Views)
Accepted Solution

vb.net external open cad drawing

2000 Views, 10 Replies
04-18-2012 03:50 PM

First off, I have working code for opening a cad drawing from an external program.  That is perfect.

 

What I'm at a loss for is how can I force a drawing that is NOT readonly to open as readonly from outside cad.    I know it's possible within cad, but that doesn't help me.

 

Can anyone help me out here?

How does your external program open a drawing? Without seeing your perfectly working code, I assume it automates AutoCAD via COM API AcadDocuments.Open() method (that is the drawing is still opened in AUTOCAD!)

 

This method has 2 optional argument to specify if the drawing to be opened as READONLY, and specify a PASSWORD if the drawing is password-protected:

 

AcadDocuments.Open "C:\....\mydrawing.dwg", True

 

opens a drawing as read-only.

*Expert Elite*
norman.yuan
Posts: 1,045
Registered: ‎04-27-2009
Message 2 of 11 (1,973 Views)

Re: vb.net external open cad drawing

04-19-2012 06:51 AM in reply to: C_Witt

How does your external program open a drawing? Without seeing your perfectly working code, I assume it automates AutoCAD via COM API AcadDocuments.Open() method (that is the drawing is still opened in AUTOCAD!)

 

This method has 2 optional argument to specify if the drawing to be opened as READONLY, and specify a PASSWORD if the drawing is password-protected:

 

AcadDocuments.Open "C:\....\mydrawing.dwg", True

 

opens a drawing as read-only.

Valued Contributor
C_Witt
Posts: 55
Registered: ‎03-22-2010
Message 3 of 11 (1,968 Views)

Re: vb.net external open cad drawing

04-19-2012 08:11 AM in reply to: C_Witt

Actually, no.  I'm using this to open drawings in cad..

 

System.Diagnostics.Process.Start(dwgpath)

 

 

 

Sounds like i should be doing the other way..   Is there some place I can get a look at working examples?

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 4 of 11 (1,953 Views)

Re: vb.net external open cad drawing

04-19-2012 10:06 AM in reply to: C_Witt

I have not tried it , but without rewriting all of your existing code, I believe you could just set the ReadOnly Attribute on the file.

Dave O.                                                                  Sig-Logos32.png
Valued Contributor
C_Witt
Posts: 55
Registered: ‎03-22-2010
Message 5 of 11 (1,951 Views)

Re: vb.net external open cad drawing

04-19-2012 10:10 AM in reply to: C_Witt

Not really an option (mainly because I still want the file to be editable at other times).

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 6 of 11 (1,947 Views)

Re: vb.net external open cad drawing

04-19-2012 10:42 AM in reply to: C_Witt

So Toggle the Attribute before opening, and toggle it back after closing.  I assumed that went without saying.

Dave O.                                                                  Sig-Logos32.png
*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 7 of 11 (1,913 Views)

Re: vb.net external open cad drawing

04-23-2012 11:20 PM in reply to: C_Witt

I didn't mean my last comment to be condescending, and I agree with Norman, that if you use the built-in COM mechanisms to open a drawing, there is a built-in way to open them read-only, but I just thought I would try to provide an alternative way, that did not require a significant change in your existing code.

 

I will admit, my suggestion requires that the users of your program have (probably a network based) permission to alter the read/write access to the file in question.  At my company, that problem could come into question under certain circumstances, but not usually when dealing with drawings.

Dave O.                                                                  Sig-Logos32.png
Valued Contributor
C_Witt
Posts: 55
Registered: ‎03-22-2010
Message 8 of 11 (1,894 Views)

Re: vb.net external open cad drawing

04-24-2012 08:15 AM in reply to: C_Witt

It's alright, everyone should let there inner Tony T out a few times.   :smileyhappy:

 

I had briefly thought of doing it that way, but wasn't able to find a way of doing it.   I've since gone the COM route.  Works great (after several google searches and code corrections).

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 11 (1,881 Views)

Re: vb.net external open cad drawing

04-24-2012 11:34 AM in reply to: C_Witt

Would be interesting to show a result of your code,

Regards,

 

Oleg

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
C_Witt
Posts: 55
Registered: ‎03-22-2010
Message 10 of 11 (1,878 Views)

Re: vb.net external open cad drawing

04-24-2012 11:41 AM in reply to: C_Witt

Not much to it, but here you go.  (though for some reason cad crashes if it's used to many times in one session... (5+ i've found))

 

Used with vanilla 2011

 

Public Sub Open_DWG ()
        Dim ACAD As Object
        Dim MyAcadPath As String = "dwgpath"
        Dim bReadOnly As Boolean = True

        On Error Resume Next
        ACAD = GetObject(, "autocad.Application")
        If Err.Number <> 0 Then
            Err.Clear()
            ACAD = CreateObject("autocad.Application")
            If Err.Number <> 0 Then
                MessageBox.Show("Failed to start", MessageBoxButtons.OK, MessageBoxIcon.Information)
                GoTo EndOpen
            End If
        End If
        'ACAD.Visible = True
        Err.Clear()
        Dim NewFile As Object = ACAD.Documents.Open(MyAcadPath, bReadOnly)
        If Err.Number <> 0 Then
            MessageBox.Show("Failed to open", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
EndOpen:
        ACAD = Nothing
        NewFile = Nothing
    End Sub

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!