Topic
  • No replies
HKrishnan
HKrishnan
25 Posts

Pinned topic DXL script to Copy Objects (along with the attributes) from one module to another module

‏2014-04-08T09:37:16Z |

Here is one script, developed by a DXL expert, which copies objects from 1 module(Source) to another module(target), which is currently open.
======================================================================================================================
pragma runLim,0

Module m = current
string targetModuleString = fullName(m)
string sourceModuleString = "SOME HARDCODED SOURCE MODULE"
Module sourceModule = read(sourceModuleString, false)
Object oObjectToCopy = current(sourceModule)
Module targetModule = edit targetModuleString
Object oObjectAsAnchor = current(targetModule)

Skip skpListOfObjsToCopy = create()
int iNumObjs = 0

void getListOfObjsToCopy(Object o, bool bWithHierarchy)
{
if ( null o ) { return }

// add this object to the list
put(skpListOfObjsToCopy, iNumObjs++, o)

if ( bWithHierarchy )
{
// add children
Object oChild
for oChild in o do
{
getListOfObjsToCopy(oChild, bWithHierarchy)
}
}
}


void copyListOfObjects(Module mTarget, Object oAnchor, bool bCreateBelow)
{
if ( null mTarget ) { return }
current = mTarget

// set up array of anchor objects
// (assume maximum number of levels is 100)
Object oAnchors[100]
int i
for i in 0:9 do
{
oAnchors[i] = null Object
}

Object oLastObjectCopied = null Object
Object oLastObjectCreated = null Object

// need to work out the difference in the levels between the two modules
int iLevelDifference = 0

// create the objects
bool bFirstObject = true
Object oSource
for oSource in skpListOfObjsToCopy do
{
// determine where to create the new object
Object oTarget = null Object
if ( null oAnchor )
{
// create new level 1 object
oTarget = create (mTarget)
}
else if ( null oLastObjectCreated )
{
if ( bCreateBelow )
{
oTarget = create(below oAnchor)
}
else
{
oTarget = create(after oAnchor)
}
}
else
{
// create in relation to previous objects
if ( level(oLastObjectCreated) < level(oSource) + iLevelDifference )
{
oTarget = create(below oAnchor)
}
else
{
oTarget = create(after oAnchors[level(oSource)])
}
}

oAnchor = oTarget
oLastObjectCreated = oTarget
oLastObjectCopied = oSource

if ( bFirstObject )
{
// level difference is worked after we have created the first new object
iLevelDifference = level(oLastObjectCreated) - level(oLastObjectCopied)
bFirstObject = false
}

// keep a record of last object created at every level
oAnchors[level(oLastObjectCreated)] = oLastObjectCreated

oLastObjectCreated."Object Heading" = richText(richTextWithOle oLastObjectCopied."Object Heading")
oLastObjectCreated."Object Text" = richText(richTextWithOle oLastObjectCopied."Object Text")
}
}

// main program
getListOfObjsToCopy(oObjectToCopy, true)
copyListOfObjects(targetModule, oObjectAsAnchor, false)

======================================================================================================================
The Script works fine, But only for the level 1 Objects, which is the copying is done for the level 1 objects and its children, Consider the below scenario.. My source Module is looking like this
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. This is a Header
1.1 Example Header
Object Text
1.1.1 Third Level Header
1.1.1.1 Fourth Level Header
Object Text
1.1.1.1.1 Fifth level Header
Object Text
Object Text

2. This is a Second Header
2.1 Example Header
Object Text
2.1.1 Third Level Header
2.1.1.1 Fourth Level Header
Object Text
2.1.1.1.1 Fifth level Header
Object Text
Object Text
2.2 This is Second Header
Object Text
Object Tex
t

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now on the target module, when I have written an DXL dialog box'd script having 2 buttons.. where click on button 1 should have this copy logic to copy the Level 1 objects and children.. and click on button 2 should copy the Level 2 objects and its children..

So I can have this script for Level 1.For Level 2., how can I search the heading "This is a Second Header" and copy the 2 level objects alone ??

Any thoughts ?

Thanks
Haresh