VBA Merge Reference

Hi All,

Trying to Merge a reference file into the Default Model and really struggling to get it working.

After reading alot on these forums and in the VBA help, I have found that there is no simple merge option in VBA, and that the best way to do it by copying, but haven't been able to find a working example. Can anyone guide me in the right direction?

Am i right in saying that i need to select everything from the reference, then use the CopyElement command to copy it to the new model?

At the moment, I can't get anything to select, let alone copy. I have done a fair bit of VBA work, but usually when selecting elements i narrow it down using ElementScanCritera, but in that there isn't an option to include all types, so im assuming by not setting one its defaulting to select everything? :/

This is a small part of a bigger project, I have already attached the Reference i want to attach and have created a new Model for it to merge into... just cant get the job done!

Thanks in advance!

Using PowerDraft 08.11.07.443 on XP

Parents
  • Unknown said:
    I have found that there is no simple merge option in VBA, and that the best way to do it by copying

    What would a 'simple merge' involve, if it's not copying from the reference to the active model?

    Unknown said:
    ... use the CopyElement command ...

    CopyElement isn't a command; it's a VBA method.  The word command has a particular meaning in MicroStation, and to maintain clarity it's best to stick with recognized nomenclature.

    Unknown said:
    I need to select everything from the reference, then use the CopyElement method to copy it to the new model?

    Yes.  Although the idiom for 'select everything from the reference' is the scanner.  Use ModelReference.Scan to collect all elements from an attachment and copy them to the active mode.  Create a CopyContext before you start the scan and use it for each copy operation.

    Unknown said:
    Usually when selecting elements i narrow it down using ElementScanCritera, but in that there isn't an option to include all types

    By default the scan criteria include everying, so you don't have to do anything to include all element types.  Be aware that the scanner will by default collect non-graphical elements, which you may not want.  Use ScanCritieria.ExcludeNonGraphical to impose that restriction.

     
    Regards, Jon Summers
    LA Solutions

  • Thanks for the reply jon

    Unknown said:

    What would a 'simple merge' involve, if it's not copying from the reference to the active model?

    Was meaning  something along the lines of

    oReference.MergeToMaster

    A one-step procedure that uses the same term as MicroStation

    My bad about the terminology... yesterday was a loooong day!!

    When I have tried to scan everything it has picked up the Non Graphical items you spoke about, so now i can get rid of those, but it didn't pick up much else. Can the selecting of the file be done after the files has been referenced into MicroStation?

    Or do i need to use the OpenDesignFileForProgram method to grab what i need?

    Is the "paste" a seperate thing or when copying does it automatically copy into the ActiveModel?

    Sorry for the barrage of questions, have been playing around with this for a while and can't much working.

  • Hi James,

    Unknown said:
    I'm sure that I've done something stupid, but for the life of me cannot figure out what!

    I found a time to make own tests and it seems the trick is in CommandState.UpdateElementDependencyState.

    This code seems to work:

    Public Sub CopyElementsFromFirstAttachment()
        Dim esc As ElementScanCriteria
        Set esc = New ElementScanCriteria
        esc.ExcludeNonGraphical
        
        Dim ee As ElementEnumerator
        Set ee = ActiveModelReference.Attachments(1).Scan(esc)
        
        Do While ee.MoveNext
            If (ee.Current.HasAnyTags) Then
                CopyElementWithTag ee.Current
            Else
                CopyElementWithoutTag ee.Current
            End If
        Loop
    End Sub
    
    Private Sub CopyElementWithoutTag(el As element)
        Dim cc As New CopyContext
        ActiveModelReference.CopyElement el, cc
        CommandState.UpdateElementDependencyState
    End Sub
    
    Private Sub CopyElementWithTag(el As element)
        Dim cc As New CopyContext
        ActiveModelReference.CopyElement el, cc
        
        Dim tagIndex As Long
        Dim aTags() As TagElement
        
        aTags = el.GetTags
        
            For tagIndex = LBound(aTags) To UBound(aTags)
            Dim sourceTag As TagElement
            Set sourceTag = aTags(tagIndex)
            ActiveModelReference.CopyElement sourceTag, cc
        Next
        
        CommandState.UpdateElementDependencyState
    End Sub
    

    With regards,

      Jan

  • Thanks for that Jan but alas it still doesn't seem to work for me in AECOsim SS6. The same as before, it copies the elements and the tagset, it even recognises that the copied tag elements are tags, but yet if I double click on them I get the error message Invalid Tag Element and the element information dialogue reports that the resource was not found.

    I read Jon's post and have solved my immediate problem which was to replicate a single host element with three associated tags from a reference attachment. I did it by copying just the host element with it's tagset as per the script above. I could then attach the copied tagset to the copied host element with it's new ID and then move each tag individually to their correct locations.

    It worked as I was working with a small number of elements, knew each Tag Definition Name and where each tag should end up which makes the code a bespoke procedure for a specific action rather than a more general "merge tags" type function. It is cumbersome and ugly but hey ho, it seems to work and enables me to move on with my assignment.

    I'm still not sure how a more general code would look like which I don't like as it leaves loose ends, but it may mean that I get to sleep at night without dreaming of ethereal orphaned tags and whimsical abandoned host elements....

    .... for the time being at least
  • Hi James,

    Unknown said:
    it still doesn't seem to work for me in AECOsim SS6.

    This is pretty new information! So far you have not mentioned you don't work in MicroStation. In the case of products like Bentley Map or AECOsim Buildig Designer there is always a threat that some operation is monitored and managed in a different way.

    Unknown said:
    The same as before, it copies ... reports that the resource was not found.

    This is weird. It works perfectly in MicroStation, I tested my code with several different references, so it's probably BD feature or something is different in your code.

    BTW Do you use VBA and not VB through Application object?

    Unknown said:
    I'm still not sure how a more general code

    I am pretty sure my code is the most general that it can be.

    More complex code can step away from CopyContext + Dependency Update mechanism and can copy TagSets before the element itself is copied and to reattach the tag to the element after the element copy. But it's far more complex, on the other hand such functionality would be more clear than a magic of CopyContext + Dependency Update.

    With regards,

      Jan

  • Opps sorry about that! I predominantly use ABD and rather naively assumed that code which was written in ABD would also work in MS as long as it did not address any ABD specific functions. It have never been an issue for me before... or perhaps it has and I just didn't realise!

    Anyway, I have retested the code in MicroStation Connect which is the only version that I have installed right now and it does seem to behave differently. Just for clarity, I took your code, created a new module and pasted it into the vba editor without change. I then attached a reference and ran the code. This is a clean version of Connect (10.00.00.25) which I have been using for evaluation only and is exactly as it was "out of the box".

    The results were different than when running in ABD. Again the tags copied ok but reported as invalid when double clicking however this time the host element did not copy at all.

    Hmmm..
  • Hi James,

    Unknown said:
    and rather naively assumed that code which was written in ABD would also work in MS

    It should work, especially in this direction, but there is always threat it's not the case in the opposite direction (code from MS run in ABD or BM). It's exceptional, but it can happen.

    Unknown said:
    Again the tags copied ok but reported as invalid when double clicking however this time the host element did not copy at all.

    Something seems to broken or substantially different on your side. Please test the attached ZIP (mvba for CE + design files), open work2d.dgn and run the macro. It works fine on my computer (be aware tags are not displayed in element information anymore(?) and Tag Review tool has to be used to display attached tags).

    With regards,

      Jan

    mvba-connect-edition.zip

  • Thanks Jan. That worked perfectly in both CE and ABD SS6...

    I am now beginning to suspect that there might be an issue with the files I am using both of which come from an external source and at least one of which may have started life as a dwg. I wonder if the issue stems from that?

    Anyway I will have to run a few tests with brand new files to see if that helps which would at least clear up the issue. It still leaves me with 200 odd files which may be temperamental, but I have my botch-job code from yesterday which should at least allow me to accomplish the task.

    Thanks again for all of your help. I greatly appreciate it.
  • I just wanted to say that in my case using Micostation CONNECT and Shared Cells elements with the instruction sc.ExcludeNonGraphical none element is selected. However, it I don't exclude the nongraphical elements I can get all of them and then I can get only the graphical ones checking if ee.Current.IsGraphical. As follows:

    Option Explicit 
    
    ' function makes copy of all graphical elements from srsRef into dstModel 
    Sub CopyGraphicalElements(dstModel As ModelReference, srcRef As Attachment) 
      ' we want to copy only graphical elements now 
      Dim sc As New ElementScanCriteria 
      'sc.ExcludeNonGraphical 
       
      ' we need copy context 
      Dim cc As New CopyContext 
      cc.LevelHandling = msdCopyContextLevelCopyIfNotFound 
    
      ' create enumerator 
      Dim ee As ElementEnumerator 
      Set ee = srcRef.Scan(sc) 
       
      ' copy elements 
      While ee.MoveNext 
        If ee.Currect.IsGraphical then
            dstModel.CopyElement ee.Current, cc 
        end if
      Wend 
      
    End Sub 
    
    ' routine makes copy of all graphical elements 
    ' from attachments into active model 
    Sub MergeAllReferences() 
      Dim att As Attachment 
      For Each att In ActiveModelReference.Attachments 
        CopyGraphicalElements ActiveModelReference, att 
      Next 

  • in my case using Micostation CONNECT and Shared Cells elements
    • This thread is many years old
    • The thread topic is VBA Merge Reference.  It is not about shared cells

    Please start a new question.

     
    Regards, Jon Summers
    LA Solutions

  • Thank you Jon,

    I know this thread is many years old and I know that the thread topic is VBA Merge Reference.

    And that was my problem, I wanted to merge a reference using VBA and I found DanPaul's code useful for that purpose, except in the case of shared cells. I don't kwon if the problem is because of the shared cells or because of the version of Microstation. The fact is that I found the solution, as I said, just commenting the sc.ExcludeNonGraphical  clause and adding the If ee.Currect.IsGraphical then clause inside the loop.

    I posted it just in case someone had the same problem.

  • Except in the case of shared cells. I don't know if the problem is because of the shared cells or because of the version of MicroStation

    Shared Cell elements and Shared Cell Definition elements

    A SharedCellElement instance requires a SharedCellDefinitionElement to exist in the same DGN file.  When you copy shared cell instances between files, you must also copy the shared cell definition. 

    The MicroStation version is inconsequential: this treatment of shared cells has been true for many generations of MicroStation.  A SharedCellDefinitionElement is non-graphical, which is why your modified scan criteria collects it.

    Your solution is a work-around.  It works by accident rather than by design.  You would have benefitted by posting a new question about copying shared cells.

     
    Regards, Jon Summers
    LA Solutions

Reply
  • Except in the case of shared cells. I don't know if the problem is because of the shared cells or because of the version of MicroStation

    Shared Cell elements and Shared Cell Definition elements

    A SharedCellElement instance requires a SharedCellDefinitionElement to exist in the same DGN file.  When you copy shared cell instances between files, you must also copy the shared cell definition. 

    The MicroStation version is inconsequential: this treatment of shared cells has been true for many generations of MicroStation.  A SharedCellDefinitionElement is non-graphical, which is why your modified scan criteria collects it.

    Your solution is a work-around.  It works by accident rather than by design.  You would have benefitted by posting a new question about copying shared cells.

     
    Regards, Jon Summers
    LA Solutions

Children
No Data