Hi, I wrote a Macro to add Entities into a Active Workspace that meet a naming criteria, and then add missing relationship lines, And I had to make this 2 seperate macros to get it to work, Can you see if I did something wrong

Hi Guys

I have a Need to have a Macro which adds Entities to an active workspace which match a naming criteria, and then add missing relationship lines to the workspace in which the parent/child tables also match a naming criteria.

What you see below as 2 macros I tried to make a single macro but found that I would get memory exceptions and it would blow up My Toad Process.

What I discovered is that the 1st macro, when it adds entities to a workspace, even though I am performing a RefreshModel, does not mark the workspace as having been updated and as such does not allow for save without me having to manually change something in the workspace.

So In Order for this to work even as 2 macros, I need to

  1. Run this macro, And when It completes, I then need to move an entity or do something on the workspace manually which then enables the Save and I can Save the model. If I do not do that then if I exit toad no changes are saved

//Macro to add entities to active workspace that start with specific characters

function Main() {
** var App = System.GetInterface(“Application”);**
** var Model = App.ActiveModel;**
** var WorkSpaceActive = App.ActiveWorkSpace;**
** var WorkingWorkspace = Model.Workspaces.GetObjectByName(WorkSpaceActive.Name);**
** var WorkSpaceMain = Model.Workspaces.GetObject(0);**
** var Log = System.CreateObject(“Log”);**
** var i, e, Entity, Shape, Entity, Link, ChildEntity, ParentEntity, WLine;**

if (Model == null) {
** Log.Information(“You Must have a Model Open to Run This Macro\nThe Process Is Ending Unsuccessfully”);**
** return;**
** } // Model == null**

if (WorkSpaceActive == null) {
** Log.Information(“You Must have a Model Open to Run This Macro\nThe Process Is Ending Unsuccessfully”);**
** return;**
** } // WorkSpaceActive == null**

Log.Information(“Starting SME_Link_Only_My_Entities_To_This_Workspace”);

Model.Lock();

** for (i = 0; i < Model.Entities.Count; i++) {**

Entity = Model.Entities.GetObject(i);
** if (**
** Entity.Name.substring(0, 4) == “trps” ||**
** Entity.Name.substring(0, 4) == “tgra” ||**
** Entity.Name.substring(0, 4) == “taoa”**
** ) {**
** if (WorkingWorkspace.ShapeList.GetObjectByName(Entity.Name) != null) {**
** Log.Information(Entity.Name + " Is Already Found in Workspace");**
** }**
** else {**
** Log.Information(Entity.Name + " Is Not Found in Workspace Adding it");**
** CreateCopyShape(WorkSpaceMain, WorkingWorkspace, Entity, 2502); //ShapeEntity**
** }**
** } // Entity If Check Names**
** } // For Entities**

** Model.UnLock();**
** Model.RefreshModel();**
** Log.Information(“Completed SME_Link_Only_My_Entities_To_This_Workspace”);**
} // Main


2) Then After I save the Model I can Run this macro to add the missing relationship lines. This appears to behave differently as when it runs if it actually adds New RElationships It does enable the Save.of the model without subsequent need for me to change something manually


//Macro to add missing relationship lines to active workspace that Have Parent and Child Tables whichstart with specific characters

function Main() {

  • ** var App = System.GetInterface(“Application”);***
  • ** var Model = App.ActiveModel;***
  • ** var WorkSpaceActive = App.ActiveWorkSpace;***
  • ** var WorkingWorkspace = Model.Workspaces.GetObjectByName(WorkSpaceActive.Name);***
  • ** var WorkSpaceMain = Model.Workspaces.GetObject(0);***
  • ** var Log = System.CreateObject(“Log”);***
  • ** var i, e, Entity, Shape, Entity, Link, ChildEntity, ParentEntity, WLine;***

if (Model == null) {

  • ** Log.Information(“You Must have a Model Open to Run This Macro\nThe Process Is Ending Unsuccessfully”);***
  • ** return;***
  • ** } // Model == null***

if (WorkSpaceActive == null) {

  • ** Log.Information(“You Must have a Model Open to Run This Macro\nThe Process Is Ending Unsuccessfully”);***

  • ** return;***

  • ** } // WorkSpaceActive == null***

  • ** Log.Information(“Starting SME_Link_Only_My_Relationship_Lines_To_This_Workspace”);***

  • ** Model.Lock();***

  • ** for (e = 0; e < WorkSpaceActive.ShapeList.Count; e++) {***

  • ** Shape = WorkSpaceActive.ShapeList.GetObject(e);***

  • ** if (Shape.ObjectType == 2502) //entity shape on WorkSpaceActive***

  • ** {***

  • ** Entity = Shape.ParentBase;***

  • ** for (r = 0; r < Entity.Relations.Count; r++) {***

  • ** Link = Entity.Relations.GetObject®;***

ChildEntity = Link.ChildEntity;

  • ** ParentEntity = Link.ParentEntity;***

Log.Information(“ParentEntity=” + ParentEntity.Name + " ChildEntity=" + ChildEntity.Name);

if (ParentEntity.Name.substring(0, 4) == “trps” ||

  • ** ParentEntity.Name.substring(0, 4) == “tgra” ||***

  • ** ParentEntity.Name.substring(0, 4) == “taoa”***

  • ** ) {***

  • ** if (ChildEntity.Name.substring(0, 4) == “trps” ||***

  • ** ChildEntity.Name.substring(0, 4) == “tgra” ||***

  • ** ChildEntity.Name.substring(0, 4) == “taoa”***

  • ** ) {***

  • ** Log.Information(“This is a Relation that if Missing will be added”);***

  • ** WLine = WorkSpaceActive.CreateWorkspaceLineByParentobject(Link, 2504); // Create Relation line***

  • ** }***

  • ** else {***

  • ** Log.Information(“This is a Relation that Will not be Added because either the Parent or Child Tables are not within the Subset of you Tables”);***

  • ** }***

  • ** }***

  • ** else {***

  • ** Log.Information(“This is a Relation that Will not be Added because either the Parent or Child Tables are not within the Subset of you Tables”);***

  • ** }***

  • ** }***

  • ** }***

  • ** }***

  • ** Model.UnLock();***

  • ** Model.RefreshModel();***

  • ** Log.Information(“Completed SME_Link_Only_My_Relationship_Lines_To_This_Workspace”);***
    } // Main

  1. Finally, You see 2 If statements in the macro above which I should have been able to make into 1 if statement with the use of bracketts and the &&, however when I use && in this IF statement I get a memory error and Toad blows up.

So this

if (ParentEntity.Name.substring(0, 4) == “trps” ||
ParentEntity.Name.substring(0, 4) == “tgra” ||
ParentEntity.Name.substring(0, 4) == "taoa"
) {
if (ChildEntity.Name.substring(0, 4) == “trps” ||
ChildEntity.Name.substring(0, 4) == “tgra” ||
ChildEntity.Name.substring(0, 4) == "taoa"
) {

Should have been allowed to be this

***if ( (ParentEntity.Name.substring(0, 4) == “trps” || ******ParentEntity.Name.substring(0, 4) == “tgra” || ***ParentEntity.Name.substring(0, 4) == “taoa” ) &&
***(ChildEntity.Name.substring(0, 4) == “trps” || ******ChildEntity.Name.substring(0, 4) == “tgra” || ******ChildEntity.Name.substring(0, 4) == “taoa” ***) )

But when i use the && above I get a memory error.

Do you guys see anything I did wrong as I would love it if it was a single Macro with the && instead of 2 macros with a manual Change and then Save in the middle.

Thanks

Anthony Vitale

Hi Anthony,

if you want mark model as modified (so save model icon will be enabled) please call

Model.SetModified(true);

To memory error. I’m not sure how substring method works with string that is shorter than 4 in your case. Could you try store substring(0,4) of parent and child entity to variable before if statement and determine that is not problem when child entity has short name (less than 4)? What kind of memory error is it?

Thanks

Daril