CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Mon Oct 22, 2018 1:19 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Sep 13, 2017 10:41 am 
Offline

Joined: Thu Apr 24, 2008 11:15 am
Posts: 24
Hi all,

I'm new to Python scripts and I am wondering if this is possible to script:

Before finishing a project I always run a couple of Find and Replace actions (with regex), for example:
Find: [\t ]+(\n)
Replace: $1

Can I make a Python script that automates this process? So it runs a couple of Find and replace actions in a row?

Will it use native CODESYS Find and replace functionality, or do I just program a regex search in Python?

Thanks


Top
   
PostPosted: Thu Sep 14, 2017 12:28 pm 
Offline
Frequent User
Frequent User

Joined: Tue Nov 13, 2012 9:20 am
Posts: 232
Hi swtroelofs.

swtroelofs wrote:
Will it use native CODESYS Find and replace functionality, or do I just program a regex search in Python?

Not at the moment but you could use the IScriptTextDocument interface to search and change the Structured Text (ST) parts of the POUs.

Here some example code for finding the textual parts:
Code:
def getFullName(obj):
   str = ""
   while obj is not None and not obj.is_root:
      str = obj.get_name() + '.' + str
      obj = obj.parent
   return str


proj = projects.primary

# Iterate through all children of the project
for child in proj.get_children(True):
   hasDecl = child.has_textual_declaration
   hasImpl = child.has_textual_implementation

   if hasDecl or hasImpl:
      print("=== Object with text: {} declaration={} implementation={}".format(getFullName(child), hasDecl, hasImpl))
      if hasDecl:
         declaration = child.textual_declaration
         print("Length of declaration: " + str(declaration.length))
         print("Content of declaration:\n<code>")
         print(declaration.text)
         print("</code>")
      if hasImpl:
         implementation = child.textual_implementation
         print("Length of implementation: " + str(implementation.length))
         print("Content of implementation:\n<code>")
         print(implementation.text)
         print("</code>")

To change the content of a textual part you use the method replace(new_content). There are more methods you can use, see the Scripting API documentation.

BR
Martin

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited