TheSwamp

Code Red => .NET => Topic started by: Robert98 on March 09, 2015, 03:17:20 PM

Title: File Size
Post by: Robert98 on March 09, 2015, 03:17:20 PM
Hi Friends
I want get size of active drawing , by this line of code in VB, but I get an Error.Please  somebody help me about correct form of this code .
Code - vb.net: [Select]
  1.  Dim length as long = new System.IO.FileInfo(path).Length
  2.  
Title: Re: File Size
Post by: BillZndl on March 09, 2015, 04:02:10 PM
what kind of error?
Title: Re: File Size
Post by: Kerry on March 09, 2015, 11:21:26 PM
What is the value of the 'path' variable ??
Title: Re: File Size
Post by: Robert98 on March 10, 2015, 02:34:19 PM
Hello
Thank you BillZndl  and Kerry for replying .The path is an active drawing folder and I want to get the size of the active drawing in KB or MB .
Routine of the path is originally written by Kean Walmsley (http://through-the-interface.typepad.com/through_the_interface/2008/03/getting-the-ful.html)
Code - vb.net: [Select]
  1. Public Sub Path()
  2.         Dim doc As Document = Application.DocumentManager.MdiActiveDocument
  3.         Dim h_app As HostApplicationServices = HostApplicationServices.Current
  4.         Dim path As String = h_app.FindFile(doc.Name, doc.Database, FindFileHint.[Default])
  5.         doc.Editor.WriteMessage(vbLf &  path)
  6. End Sub
Title: Re: File Size
Post by: BillZndl on March 10, 2015, 03:27:53 PM
You did say what error you are getting but if it's eFilerError,
most likely you're running your code in a new/unsaved drawing.
If I check for an unsaved drawing, the code works find.
Sorry but I only know C#.

Code - C#: [Select]
  1.  int title = Convert.ToInt32(AcadApp.GetSystemVariable("DWGTITLED"));
  2.  
  3.             if (title > 0 )
  4.             {
  5.                 string path = h_app.FindFile(doc.Name, doc.Database, FindFileHint.Default);                                    
  6.                 FileInfo info = new FileInfo(path);
  7.                 size = info.Length;
  8.                 ed.WriteMessage("\nPath: " + path);
  9.                 ed.WriteMessage("\nFileSize: " + size);
  10.             }
  11.             else
  12.             {
  13.                 ed.WriteMessage("\nFile not found");
  14.             }
  15.  
Title: Re: File Size
Post by: gile on March 10, 2015, 03:31:44 PM
You can try this way (if the file isn't already saved, db.Filename returns the template path):

Code - C#: [Select]
  1. Document doc = Application.DocumentManager.MdiActiveDocument;
  2. Database db = doc.Database;
  3. Editor ed = doc.Editor;
  4. ed.WriteMessage("\n{0} Ko", new System.IO.FileInfo(db.Filename).Length / 1024);
Title: Re: File Size
Post by: BillZndl on March 10, 2015, 04:25:37 PM
You can try this way (if the file isn't already saved, db.Filename returns the template path):

Much better! 8)
Title: Re: File Size
Post by: Robert98 on March 11, 2015, 12:35:56 PM
Hello
Thank you BillZndl  and gile for replying .
I used both suggestions. gile's code worked properly but, when I run BillZndl's I get an error like this one :

Quote
Error   3   'GetSystemVariable' is not a member of 'Autodesk.AutoCAD.ApplicationServices.Document'.
Error   4   'EditorWriteMessage' is not a member of 'Autodesk.AutoCAD.ApplicationServices.Document'.   

Here is my code:
Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports System.IO


Public Class Class1
    <CommandMethod("PS")> _
    Public Sub DrawingPath()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim h_app As HostApplicationServices = HostApplicationServices.Current
        Dim title As Integer = Convert.ToInt32(doc.GetSystemVariable("DWGTITLED"))

        If title > 0 Then
            Dim path As String = h_app.FindFile(doc.Name, doc.Database, FindFileHint.[Default])
            Dim info As New FileInfo(path)
            Dim size As Long = info.Length
            doc.Editor.WriteMessage(vbLf & "Path: " & path)
            doc.EditorWriteMessage(vbLf & "FileSize: " & size)
        Else
            doc.Editor.WriteMessage(vbLf & "File not found")
        End If

    End Sub
End Class

Title: Re: File Size
Post by: Jeff_M on March 11, 2015, 12:51:02 PM
Robert, the GetSystemVariable is applicable to the Application, not the Document (re-read Bill's post).
You omitted a '.' in the line just above the Else.
Title: Re: File Size
Post by: BillZndl on March 11, 2015, 01:27:09 PM
Yup,
The using statement for AcadApp is:
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

Then:
AcadApp.GetSystemVariable("DwgTitled");

Thanks Jeff!

Title: Re: File Size
Post by: MexicanCustard on March 12, 2015, 07:48:47 AM
Yup,
The using statement for AcadApp is:
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

Then:
AcadApp.GetSystemVariable("DwgTitled");

Thanks Jeff!


You shouldn't wrap the Application in a Using statement.  The Application scope is outside your method and AutoCAD is handling its disposal.
Title: Re: File Size
Post by: gile on March 12, 2015, 08:49:07 AM
Yup,
The using statement for AcadApp is:
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

Then:
AcadApp.GetSystemVariable("DwgTitled");

Thanks Jeff!


You shouldn't wrap the Application in a Using statement.  The Application scope is outside your method and AutoCAD is handling its disposal.

I think the 'using' here isn't a 'statement', rather a 'directive' used to define an alias (see here (https://msdn.microsoft.com/en-us/library/zhdeatwt.aspx))
Title: Re: File Size
Post by: BillZndl on March 12, 2015, 09:46:49 AM

I think the 'using' here isn't a 'statement', rather a 'directive' used to define an alias (see here (https://msdn.microsoft.com/en-us/library/zhdeatwt.aspx))

Er, yes, I should have said directive not statement. My bad.
Learn something new everyday!
Thanks.
Title: Re: File Size
Post by: Jeff H on March 12, 2015, 11:39:54 AM
Yup,
The using statement for AcadApp is:
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

Then:
AcadApp.GetSystemVariable("DwgTitled");

Thanks Jeff!
Your Welcome