Results 1 to 2 of 2

Thread: Write to File when Saving model

  1. #1
    All AUGI, all the time davidcobi's Avatar
    Join Date
    2005-03
    Location
    Greater Los Angeles
    Posts
    546
    Login to Give a bone
    0

    Default Write to File when Saving model

    Not certain why this addin fails to write a log file in <server location>.
    Any suggestions would be appreciated.

    Code:
    using System;
    using System.IO;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using System.Collections.Generic;
    using Autodesk.Revit.DB.Events;
    
    namespace TwentyMileWarning
    {
        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
        public class ThisDocument : IExternalApplication
        {
            
            // Create strings.
            string r1 = "FALSE";
            string r2 = "FALSE";
            public Result OnStartup(UIControlledApplication application)
            {
                try
                {
                    application.ControlledApplication.DocumentOpened
                        += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocOpen);
                }
                catch
                {
                    return Result.Failed;
                }
                return Result.Succeeded;
            }
            public Result OnShutdown(UIControlledApplication application)
            {
                try
                {
                    application.ControlledApplication.DocumentSaving
                        += new EventHandler<Autodesk.Revit.DB.Events.DocumentSavingEventArgs>(DocClose);
                }
                catch
                {
                    return Result.Failed;
                }
                return Result.Succeeded;
            }
    
            private void DocOpen(object sender, DocumentOpenedEventArgs e)
            {
                    // get the current model
                    Document doc = e.Document;
    
                    // get a list of all the model's warnings
                    IList<FailureMessage> warnings = doc.GetWarnings();
    
                    // loop through each warning
                    foreach (FailureMessage fmsg in warnings)
                    {
                        string s1 = fmsg.GetDescriptionText();
                        string s2 = "20 mile";
                        bool b = s1.Contains(s2);
                        if (b)
                        {
                        r1 = "TRUE";
    
                            // show dialog if 20 mile limit warning exists
                            TaskDialog.Show("WARNING", fmsg.GetDescriptionText());
                        }
                    }
            }
            private void DocClose(object sender, DocumentSavingEventArgs e)
            {
                // get the current model
                Document doc2 = e.Document;
    
                // get system time and system username
                string GetTimestamp(DateTime value)
                {
                    return value.ToString("yyyyMMddHHmmssffff");
                }
                string timeStamp = GetTimestamp(DateTime.Now);
                string userName = Environment.UserName;
    
                // get a list of all the model's warnings
                IList<FailureMessage> warnings2 = doc2.GetWarnings();
    
                // loop through each warning
                foreach (FailureMessage fmsg2 in warnings2)
                {
                    string s1a = fmsg2.GetDescriptionText();
                    string s2a = "20 mile";
                    bool b2 = s1a.Contains(s2a);
                    if (b2)
                    {
                        r2 = "TRUE";
                    }
                }
                if ((r1 == "TRUE") || (r2 == "TRUE"))
                {
                    // directory location
                    var dir = @"<server location>";
    
                    // if directory does not exist, create
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
    
                    // use Path.Combine to combine strings to a path
                    File.WriteAllText(Path.Combine(dir, timeStamp + "_log.csv"), userName + "," + doc2.PathName + "," + r1 + "," + r2);
    
                }
            }
        }
    }
    Last edited by dduarte; 2019-04-16 at 07:04 PM.

  2. #2
    All AUGI, all the time davidcobi's Avatar
    Join Date
    2005-03
    Location
    Greater Los Angeles
    Posts
    546
    Login to Give a bone
    0

    Default Re: Write to File when Saving model

    This appears to work now. Any suggestions appreciated.

    Code:
    using System;
    using System.IO;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using System.Collections.Generic;
    using Autodesk.Revit.DB.Events;
    
    namespace TwentyMileWarning
    {
        [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
        public class ThisDocument : IExternalApplication
        {
            
            // Create strings
            string r1 = "FALSE";
            string r2 = "FALSE";
            public Result OnStartup(UIControlledApplication application)
            {
                try
                {
                    application.ControlledApplication.DocumentOpened
                        += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocOpen);
    
                    application.ControlledApplication.DocumentSaving
                        += new EventHandler<Autodesk.Revit.DB.Events.DocumentSavingEventArgs>(DocSave);
    
                    application.ControlledApplication.DocumentClosing
                        += new EventHandler<Autodesk.Revit.DB.Events.DocumentClosingEventArgs>(DocClosing);
                }
                catch
                {
                    return Result.Failed;
                }
                return Result.Succeeded;
            }
            public Result OnShutdown(UIControlledApplication application)
            {
                try
                {
                    application.ControlledApplication.DocumentOpened
                        -= new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(DocOpen);
    
                    application.ControlledApplication.DocumentSaving
                        -= new EventHandler<Autodesk.Revit.DB.Events.DocumentSavingEventArgs>(DocSave);
    
                    application.ControlledApplication.DocumentClosing
                        -= new EventHandler<Autodesk.Revit.DB.Events.DocumentClosingEventArgs>(DocClosing);
                }
                catch
                {
                    return Result.Failed;
                }
                return Result.Succeeded;
            }
    
            private void DocOpen(object sender, DocumentOpenedEventArgs e)
            {
    
                    // get the current model
                    Document doc = e.Document;
    
                    // get a list of all the model's warnings
                    IList<FailureMessage> warnings = doc.GetWarnings();
    
                    // loop through each warning
                    foreach (FailureMessage fmsg in warnings)
                    {
                        string s1 = fmsg.GetDescriptionText();
                        string s2 = "20 mile";
                        bool b = s1.Contains(s2);
                        if (b)
                        {
                        r1 = "TRUE";
    
                            // show dialog if 20 mile limit warning exists
                            TaskDialog.Show("WARNING", fmsg.GetDescriptionText());
                        }
                    }
            }
            private void DocSave(object sender, DocumentSavingEventArgs e)
            {
                // get the current model
                Document doc2 = e.Document;
    
                // get system time and system username
                string GetTimestamp(DateTime value)
                {
                    return value.ToString("yyyyMMddHHmmssffff");
                }
                string timeStamp = GetTimestamp(DateTime.Now);
                string userName = Environment.UserName;
    
                // get a list of all the model's warnings
                IList<FailureMessage> warnings2 = doc2.GetWarnings();
    
                // loop through each warning
                foreach (FailureMessage fmsg2 in warnings2)
                {
                    string s1a = fmsg2.GetDescriptionText();
                    string s2a = "20 mile";
                    bool b2 = s1a.Contains(s2a);
                    if (b2)
                    {
                        r2 = "TRUE";
                    }
                }
    
                if ((r1 == "TRUE") || (r2 == "TRUE"))
                {
                    // directory location
                    var dir = @"<server location>";
    
                    // if directory does not exist, create
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
    
                    // use Path.Combine to combine strings to a path
                    File.WriteAllText(Path.Combine(dir, timeStamp + "_log.csv"), userName + "," + doc2.PathName + "," + r1 + "," + r2);
                    // Clear strings
                    r2 = "FALSE";
    
                }
            }
            private void DocClosing(object sender, DocumentClosingEventArgs e)
            {
                    // Clear strings
                    r1 = "FALSE";
                    r2 = "FALSE";
            }
    
        }
    }

Similar Threads

  1. Write protection of a file
    By sivampeta_dheeraj in forum AutoCAD General
    Replies: 10
    Last Post: 2011-08-02, 12:10 AM
  2. File write problem with NWD- /NWC-file type
    By bma in forum NavisWorks - General
    Replies: 3
    Last Post: 2010-08-31, 09:43 PM
  3. Replies: 10
    Last Post: 2007-10-09, 01:11 PM
  4. How to write new menu file or update the existing menu in AutoCAD 2006
    By Partha Ghosh in forum CAD Management - General
    Replies: 1
    Last Post: 2007-09-30, 01:45 PM
  5. ADT 2007 - Failure to write when saving dwg files.
    By mike.pedigo in forum ACA General
    Replies: 2
    Last Post: 2006-04-18, 07:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •