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.
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:
Answer Verified By: Rob Golding
Unknown said: something along the lines of oReference.MergeToMaster
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...
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
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 SubI 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!
Unknown said:I still cannot copy tags...
I've started a new thread Copying Tags between Files. Post further questions to that thread.
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
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