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.

  • Jon suggested you to use correct terminology, but you still mention some 'paste' method and so. In VBA there is no 'paste' method - it is really a separate thing. VBA is a scripting/programming language, so all tasks need to be programmed, or you can invoke a MicroStation command.

    Merging reference to master in VBA is simple 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
        dstModel.CopyElement ee.Current, cc
      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
    End Sub

    Answer Verified By: Rob Golding 

  • Unknown said:

    something along the lines of oReference.MergeToMaster

    Please don't take my comment below as a criticism — it's intended to provoke you to think more deeply about the purpose of programming.

    Analyse what you've written in the context of my earlier comment.  What would your Attachment.MergeToMaster do if it's not scanning and copying?

    As a programmer you have to peer and poke around inside MicroStation.  That peering and poking occurs at a much more intimate level of detail than seen by a regular user.

     
    Regards, Jon Summers
    LA Solutions

  • Unknown said:

    something along the lines of oReference.MergeToMaster

    Please don't take my comment below as a criticism — it's intended to provoke you to think more deeply about the purpose of programming.

    Analyse what you've written in the context of my earlier comment.  What would your Attachment.MergeToMaster do if it's not scanning and copying?

    As a programmer you have to peer and poke around inside MicroStation.  That peering and poking occurs at a much more intimate level of detail than seen by a regular user.

    [/quote]

    I haven't taken anything as criticism, the reason why i come onto these forums is to learn from experts like yourself, then hopefully someday i can help someone else.

    Dan's post above has done exactly what i wanted. My code that i pieced together from the VBA Help examples, forum posts etc wasn't far off, infact it was missing something pretty small but enough for it not to work.

    But in saying that I now have another problem with the bigger project as described above... but its not to do with merging so i'll make a new thread as i have the answer i was looking for.

  • I have been experimenting a little bit with this and have run into a problem.

    I'm trying to merge a file containing a tagset into the active design file in VBA using code similar to this. It seems that the tagset is created in the active model and the tags are copied as tags yet when I try to edit them, all I get is an error message saying "Invalid Tag Element". Furthermore, when I look at the element information, the Contents reads "Resource not found":

    Does anybody have any tips for what I may be doing wrong?  I'm sure it is something simple which I have overlooked but I am tearing out what little of my hair is left...

  • Hi James,

    this is quite old (3 years) discussion about general "reference merge using VBA", so in my opinion it would be better to post a new question specifically about the problem with wrong tag.

    If you will add propert subject (see Forum Best Practices) and will add information what version of MicroStation do you use, or even will add some code snippet, it will be great question.

    With regards,

    Jan
  • Hi James,

    without seeing your code it's hard to investigate what is wrong.

    Quite often problem when elements are copied from another file is CopyContext is not used. You can check CopyContext object description in MicroStation VBA help, there is even a code snippet how tags should be copied.

    With regards,

    Jan
  • Hi Jan,

    Here is the code so far:

    Sub CopyGraphicalElements(ByVal sourceRef As Attachment)
      
        Dim oScanCriteria As New ElementScanCriteria
        oScanCriteria.ExcludeNonGraphical
        
        Dim oEnum As ElementEnumerator
        Set oEnum = sourceRef.Scan(oScanCriteria)
         
        While oEnum.MoveNext
            CopyElementWithTags oEnum.Current
        Wend
      
    End Sub
    
    Sub CopyElementWithTags(sourceElement As Element)
        Dim oCC As New CopyContext
        
        ActiveModelReference.CopyElement sourceElement, oCC
        
        Dim tagIndex As Long
        Dim aTags() As TagElement
        
        aTags = sourceElement.GetTags
        
        For tagIndex = LBound(aTags) To UBound(aTags)
            Dim sourceTag As TagElement
            Set sourceTag = aTags(tagIndex)
            ActiveModelReference.CopyElement sourceTag, oCC
        Next
    End Sub

    I used copy context exactly as per the example but still cannot copy usable tags... I'm sure that I've done something stupid, but for the life of me cannot figure out what!
  • Reply
    • Hi Jan,

      Here is the code so far:

      Sub CopyGraphicalElements(ByVal sourceRef As Attachment)
        
          Dim oScanCriteria As New ElementScanCriteria
          oScanCriteria.ExcludeNonGraphical
          
          Dim oEnum As ElementEnumerator
          Set oEnum = sourceRef.Scan(oScanCriteria)
           
          While oEnum.MoveNext
              CopyElementWithTags oEnum.Current
          Wend
        
      End Sub
      
      Sub CopyElementWithTags(sourceElement As Element)
          Dim oCC As New CopyContext
          
          ActiveModelReference.CopyElement sourceElement, oCC
          
          Dim tagIndex As Long
          Dim aTags() As TagElement
          
          aTags = sourceElement.GetTags
          
          For tagIndex = LBound(aTags) To UBound(aTags)
              Dim sourceTag As TagElement
              Set sourceTag = aTags(tagIndex)
              ActiveModelReference.CopyElement sourceTag, oCC
          Next
      End Sub

      I used copy context exactly as per the example but still cannot copy usable tags... I'm sure that I've done something stupid, but for the life of me cannot figure out what!
    Children
    • Unknown said:
      I still cannot  copy tags...

      I've started a new thread Copying Tags between Files.  Post further questions to that thread.

       
      Regards, Jon Summers
      LA Solutions

    • 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.