Error in program

Error in program

Anonymous
Not applicable
1,692 Views
12 Replies
Message 1 of 13

Error in program

Anonymous
Not applicable

Dear all,

 

i am devoioping one document change event but i am struct at errors and warnings, looking suggestions from api experts.

for the same i have attached code and error image for all of yours reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

namespace Event Manager
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]

public class Class1 : IExternalCommand
{
       void ControlledApplication_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
        {
            string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "CommandLogging.csv");
            using (StreamWriter sw = new StreamWriter(file, true))
            {
                foreach (string transactionName in e.GetTransactionNames())
                {
                    sw.Write(DateTime.Now + "," + Utils.username + "," + transactionName);
                    sw.Write(",ADDED," + idCollectionToString(e.GetDocument(), e.GetAddedElementIds()));
                    sw.Write(",DELETED," + idCollectionToString(e.GetDocument(), e.GetDeletedElementIds()));
                    sw.Write(",MODIFIED," + idCollectionToString(e.GetDocument(), e.GetModifiedElementIds()));
                    sw.Write(Environment.NewLine);
                }
            }
        }

        private string idCollectionToString(Document doc, ICollection<ElementId> coll)
        {
            string ret = "";
            foreach (ElementId id in coll)
            {
                Element e = doc.GetElement(id);
                string name = "";
                if (e != null)
                {
                    string catName = "";
                    if (e.Category != null)
                        catName = e.Category.Name;
                    if (e.Name != "" && catName != "")
                        catName += ": ";
                    name = "(" + catName + e.Name + ")";
                }
                ret += id.IntegerValue.ToString() + name + ",";
            }
            return ret;
        }
    }
}

Thanks & Regards

Amith

0 Likes
1,693 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable

Hi,

first, your need to add an object that support the IExternalCommand interface. 

example :

 

public class Class1 : IExternalCommand {
public Result Execute(ExternalCommand commandData, ref string message, ElementSet elementset){

//CODE//

return Result.Succeeded;
}
}

 

and you need to add : using System.IO;  for use StreamWriter

Message 3 of 13

rosalesduquej
Alumni
Alumni

Dear Amith,

 

After checking your screenshot and reproducing your code I can see the cause of your errors being triggered. 

I added a screenshot with comments on how to solve them. 

 

I hope it helps. 

 

Cheers,



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
0 Likes
Message 4 of 13

Anonymous
Not applicable

Thanks alot for both of your reply i tried with solution its working but now facing another error, for the same i am reattaching updated code with error screen shot please sugest me to solove the issue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.IO;
using System.Data;
using System.Configuration;

namespace event

{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
   
   
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet Elements)
        {
        }
        void ControlledApplication_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
        {
            string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "CommandLogging.csv");
            using (StreamWriter sw = new StreamWriter(file, true))
            {
                foreach (string transactionName in e.GetTransactionNames())
                {
                    sw.Write(DateTime.Now + "," + Utils.username + "," + transactionName);
                    sw.Write(",ADDED," + idCollectionToString(e.GetDocument(), e.GetAddedElementIds()));
                    sw.Write(",DELETED," + idCollectionToString(e.GetDocument(), e.GetDeletedElementIds()));
                    sw.Write(",MODIFIED," + idCollectionToString(e.GetDocument(), e.GetModifiedElementIds()));
                    sw.Write(Environment.NewLine);
                }
            }
        }

        private string idCollectionToString(Document doc, ICollection<ElementId> coll)
        {
            string ret = "";
            foreach (ElementId id in coll)
            {
                Element e = doc.GetElement(id);
                string name = "";
                if (e != null)
                {
                    string catName = "";
                    if (e.Category != null)
                        catName = e.Category.Name;
                    if (e.Name != "" && catName != "")
                        catName += ": ";
                    name = "(" + catName + e.Name + ")";
                }
                ret += id.IntegerValue.ToString() + name + ",";
            }
            return ret;
        }
    }
}

That identifier error in namespace and i dont know what is warnings.

 

Thanks & Regards

Amith

 

 

0 Likes
Message 5 of 13

Anonymous
Not applicable
Hi,
yes, you cannot use "event" for your namespace, writte another word
0 Likes
Message 6 of 13

Anonymous
Not applicable

i got your point after changing the name it will show me other errors for the same i attached another screen shot.

 

thanks 

amith

0 Likes
Message 7 of 13

Anonymous
Not applicable

because  public Result Execute is empty

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet Elements)
{

//CODE//

return Result.Succeeded;
}



0 Likes
Message 8 of 13

Anonymous
Not applicable

Dear neoris,

 

As per your above reply i will tried but i am getting struct on somewhere if you dont mind can you change wherever it required in above mentioned program because i am new to this api ,i cannot undersatnd where to give return Result.Succeeded in this program,its my request to you.

 

Thanks&Regards

amith

 

0 Likes
Message 9 of 13

Anonymous
Not applicable

Hi,

 

Please ignore above reply,i got your point and solved as per your instruction in above reply from you, one more error in the program can you look at that.

"The name 'Utils' does not exist in the current context"

how to solove this error

 

Thanks

Amith

0 Likes
Message 10 of 13

Anonymous
Not applicable

"return Result.Succeeded;" is always in the end of your public Result Execute(...), because this method need to return something for work.

 

Your program is not complete for what you want .
I invite you to read the sample in Revit SDK named " documentChanged " , it show you how to track changes in the Revit application.

 

 

0 Likes
Message 11 of 13

Anonymous
Not applicable

Hi,

 

That one is now soloved but i have another error "The name 'Utils' does not exist in the current context" what is this error, how to solove this one

 

Thanks

Amith

0 Likes
Message 12 of 13

Anonymous
Not applicable

Utils.username is a function created for retrieve the username of your revit session but here you just call the function whitout writte the method in your code.

 

Utils is a class , username is a function, it's like a shortcut , in Utils class you writte a lot of function/method that you need in all your programs, and when you want you call the function in your app.

 

delete Utils.username for solve your program, but as i say on top, your program isn't complete for work.

 

sw.Write(DateTime.Now + "," + transactionName);
Message 13 of 13

Anonymous
Not applicable

Hi,

 

After debug i will created manifest file in notepad and save as addin file format  but after checking with revit there is no external command in addin what is the reason for cause.

 

Thanks & Regards

Amith

0 Likes