Faster C++ Development

I have been programming in MicroStation with C++ for a bit and have been trying to spend more time in C++ than in VBA.  This helps me become more familiar with the API and creates a larger C++ codebase to use.  C++ development is slower, but my goal is to become better at C++.

I found that I could create "mini" apps that implement SDK functions (no gui stuff yet).  I based these apps off of the "basic" example delivered in the SDK.  But because of the larger file structure, it was inconvenient to re-create that file structure for other apps (.mke, .r, .mki, .mt...).  I was left switching my code in the .cpp file for basic and just saving the sourcecode in a text file.  

Anyways, I made a solution of sorts to help with quicker C++ app development.  It is some code I wrote in Python that just replaces the appname correctly throughout the app, and copies all of the files to a new directory with a new designated app name.  I am sure you could probably do this through makefiles (I am not sure how), but it was easy enough to do in Python.

This is convenient, because I can quickly copy a fairly empty app template into new apps.  These apps then are saved with custom names for later use in MicroStation.

I thought that the app was extremely convenient for me, and that other new coders could benefit from the sourcecode.

The following code is in Python 3 (not Python 2)!  You may need to change the baseFiles variable to match your system (but I think that filepath is fairly standard).

from os import listdir
import os
from os.path import join
import shutil
import itertools
baseFiles = r"C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries)"

def getExistingFolders():
    folders = []
    for folder in listdir(baseFiles):
        folders.append(folder)
    return folders

existingFolders = getExistingFolders()
folderName = input("Please Enter the Base Program's Name\n")

def getBaseFolder():
    for folder in listdir(baseFiles):
        if folder == folderName:
            baseFolder = os.path.join(baseFiles,folder)
            return baseFolder
def getAllFilesToBeReplaced():
    files = []
    for file in listdir(baseFolder):
        if file.find(".") == -1:
            pathDepth2 = os.path.join(baseFolder,file)
            for fileDepth2 in listdir(pathDepth2):
                files.append(os.path.join(baseFolder,pathDepth2,fileDepth2))
        else:
            files.append(os.path.join(baseFolder,file))
    return files
 
def copyFile(src, dest):
    try:
        shutil.copy(src, dest)
    # eg. src and dest are the same file
    except shutil.Error as e:
        print('Error: %s' % e)
    # eg. source or destination doesn't exist
    except IOError as e:
        print('Error: %s' % e.strerror)
        
baseFolder = getBaseFolder()
files = getAllFilesToBeReplaced()
newString = folderName
while newString in existingFolders:
    newString = input("Please Enter New Program Name\n")

newFolderPath = os.path.join(baseFiles,newString)

#Now Create New Folder
def getFolders(path):
	files = []
	for file in listdir(path):
		file_path = os.path.join(path,file)
		if os.path.isdir(file_path):
			files.append(file_path)
			for x in getFolders(file_path):
				files.append(x)
	return(files)
    
folders = getFolders(baseFolder)


def copy_and_create_new_files_and_folders():
    new_files = []
    os.makedirs(newFolderPath)
    for folder in folders:
        os.makedirs(folder.replace(folderName,newString))
    for file in files:
        copyFile(file,file.replace(folderName,newString))
        new_files.append(file.replace(folderName,newString))
    return(new_files)

new_files = copy_and_create_new_files_and_folders()
def cc(s):
    return (''.join(t) for t in itertools.product(*zip(s.lower(), s.upper())))
	    
for file in new_files:
    opened_file = open(file,'r')
    file_contents = opened_file.read()
    all_combos = list(cc(folderName))
    new_file_contents = file_contents #this will change in the next for loop
    for combo in all_combos:
        new_file_contents = new_file_contents.replace(combo,newString)
    opened_file.close()
    write_new_file = open(file,'w')
    write_new_file.write(new_file_contents)
    write_new_file.close()

An example output from using this program might look like this:

Please Enter the Base Program's Name
basic
Please Enter New Program Name
NewAppName

This would copy all of the files (even nested) in the basic folder, then replace the name "basic" with "NewAppName" in the necessary places. You could then compile NewAppName with bmake immediately.

I use this mini app all the time for speeding up development as an excuse for using more C++ from day to day. I hope it is useful to someone else.

Parents Reply Children