Error txt file from script

Hi all,

I try to build a script that create something like a journal after run the script.
Thanks to @c.poupin this solution works:

This code works fine in a new script with a few nodes but when I try to use it in a older script it doesn’t work. (big script)
Does someone know how I can control this?

import clr #.NET Laden
import sys #sys is de fundamentele Python bibliotheek

import os.path
import datetime
from System import EventHandler, Uri, Environment
from System.Collections.Generic import List

clr.AddReference('DynamoCoreWpf') 
clr.AddReference('DynamoCore')
clr.AddReference('DynamoRevitDS')
clr.AddReference('DynamoServices')
clr.AddReference('DSCoreNodes')
from DSCore import *
import Dynamo 
from Dynamo.Graph.Workspaces import *
from Dynamo.Graph.Nodes import *
from Dynamo.Models import *
	
username = Environment.UserName
my_path = "Y:\\Dynamo\\Journal files\\" + username

def EventResumeNodes(sender, e):
	try:
		ResumeNodes()
	except Exception as ex:
		print('ERROR TRACEBACK' + str(ex))
	
def ResumeNodes(): 
	global dynamoRevit
	global currentWorkspace
	resume_file_path = my_path + ".txt"
	lstError = []
	for i in currentWorkspace.Nodes:
		if i.State == ElementState.Warning:
			lstError.append("###############################")
			lstError.append("Node : {}".format(i.Name))
			lstError.append("Error : {}".format(i.ToolTipText))
	with open(resume_file_path, 'w') as f:
		for line in lstError:
			f.write("{}\n".format(line))
	dynamoRevit.RevitDynamoModel.EvaluationCompleted -= EventHandler[EvaluationCompletedEventArgs](EventResumeNodes)





dynamoRevit = Dynamo.Applications.DynamoRevit()
currentWorkspace = dynamoRevit.RevitDynamoModel.CurrentWorkspace
dynamoRevit.RevitDynamoModel.EvaluationCompleted += EventHandler[EvaluationCompletedEventArgs](EventResumeNodes)

OUT = my_path

Hello,
Which version of Revit ? Dynamo ?

@c.poupin Revit 2020 - 2022 and dynamo version 2.6
(We don’t use 2022 yet but it will be used soon included the new dynamo version)

Can you share a sample problematic dyn file ?

@c.poupin Sure, you find the node below the stamp.

BTW don’t forget to change the filepath location.
00-_Sheets per verdieping.dyn (445.7 KB)

It was an encoding error, try this version

import clr #.NET Laden
import sys #sys is de fundamentele Python bibliotheek
import System
import os.path
import datetime
from System import EventHandler, Uri, Environment
from System.Collections.Generic import List

clr.AddReference('DynamoCoreWpf') 
clr.AddReference('DynamoCore')
clr.AddReference('DynamoRevitDS')
clr.AddReference('DynamoServices')
clr.AddReference('DSCoreNodes')
from DSCore import *
import Dynamo 
from Dynamo.Graph.Workspaces import *
from Dynamo.Graph.Nodes import *
from Dynamo.Models import *
	
username = Environment.UserName
my_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
resume_file_path = my_path + "\\" + username + "_log_error.txt"

def EventResumeNodes(sender, e):
	try:
		ResumeNodes()
	except Exception as ex:
		import traceback
		error = traceback.format_exc()
		WriteErrors(['ERROR on Journal File Python Node', error])
	
def ResumeNodes(): 
	global currentWorkspace
	lstError = []
	for i in currentWorkspace.Nodes:
		if i.State == ElementState.Warning:
			lstError.append("###############################")
			lstError.append("Node : {}".format(i.Name))
			lstError.append("Error : {}".format(i.ToolTipText))
	WriteErrors(lstError)
	
def WriteErrors(lstError):
	global dynamoRevit
	global resume_file_path
	if System.IO.File.Exists(resume_file_path):
		System.IO.File.WriteAllText(resume_file_path, System.String.Empty)
	sb2 = System.Text.StringBuilder()
	for line in lstError:
		sb2.Append("{}\n".format(line))
	System.IO.File.AppendAllText(resume_file_path, sb2.ToString())
	sb2.Clear()
	# remove Event
	dynamoRevit.RevitDynamoModel.EvaluationCompleted -= EventHandler[EvaluationCompletedEventArgs](EventResumeNodes)

dynamoRevit = Dynamo.Applications.DynamoRevit()
currentWorkspace = dynamoRevit.RevitDynamoModel.CurrentWorkspace
dynamoRevit.RevitDynamoModel.EvaluationCompleted += EventHandler[EvaluationCompletedEventArgs](EventResumeNodes)

OUT = my_path

Thank you @c.poupin !
Was it the path? I want to use another path than my documents

juste replace this
resume_file_path = my_path + "\\" + username + "_log_error.txt"
by
resume_file_path = "Y:\\Dynamo\\Journal files\\" + username + "_log_error.txt"

1 Like

Got it! Thanks :slight_smile: