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
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
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
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.
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).
mvba-connect-edition.zip
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
Victor Montes C?mara said: in my case using Micostation CONNECT and Shared Cells elements
Please start a new question.
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.
Victor Montes C?mara said: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
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.
SharedCellElement
SharedCellDefinitionElement
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.