[V8i - VBA] CopyElement problems

Good morning friends,

in a 3D model, I have some 3D objects (Cells).  I did "Visible Edge Export" of each of them on the XY, XZ and YZ planes. And then Flatten Curve.

My desire is to copy each of these cells (They are cells because the lines obtained with Flatten Curve have been grouped - GROUP SELECTION command) from the 3d model of source Dgn to a target Dgn (2D model) using OpenDesignFileForProgram.

I read in the Microstation VBA Help that, with OpenDesignFileForProgram, is not possible make use of AddElement (is it correct?).

Then I utilised CopyElement, but I got some problems:

1. CellElements are 100 times greater (Both files, source and target, have the same working unit and active scale is set to 1 for x, y and z)

2. I got, in the target file, only one cell for each originary 3D cells in the right "persective" (please see the attached image) : top and left are wrong.

3. Cells Position, in the target file are not the asked ones (I suspected this problem could be linked to n.1 problem)

I tried to do a lot of attempts to solve those problems, without luck...

I insert the last one here (where I also added cells to source file too, to see if AddElement (Instead of CopyElement) works well...and all was right).

Sub CopyToSmartSketch()
    Dim myDgn As DesignFile
    Dim myCopyContext As New CopyContext
    
    ActiveDesignFile.Models("Default").AddElement myCellToExchange
    
    Set myDgn = OpenDesignFileForProgram("C:\Users\Paolo\Desktop\Celle\smartSketch.dgn", False)
    
    myCopyContext.ChangeUnits = True
    myCopyContext.MatchDimensionToDestination = True
    myCopyContext.LevelHandling = msdCopyContextLevelByUserPreference
    myCopyContext.ViewForFlattening = myPlane
    
    myDgn.Models("Default").CopyElement myCellToExchange, myCopyContext
    
    myCellToExchange.Origin.X = myOriginX
    myCellToExchange.Origin.Y = myOriginY
    
    myCellToExchange.Rewrite
    myCellToExchange.Redraw
    
    myDgn.RewriteLevels
    myDgn.Save
    myDgn.Close
End Sub

I searched on the web: I found a LA Solution to copy objects from 3D model to a 2D one, but (I didn't try it) I would like to write my own one.

Thanks a lot.

Best regards, 
Paolo

  • My desire is to copy each of these cells using OpenDesignFileForProgram.

    Prefer OpenDesignFile

    Prefer OpenDesignFile to OpenDesignFileForProgram! Read this blog.

    I read in the Microstation VBA Help that, with OpenDesignFileForProgram, is not possible make use of AddElement (is it correct?).

    Why should it not be correct? Read the blog for reasons not to use OpenDesignFileForProgram.

    My desire is to copy each of these cells from the 3d model of source Dgn to a target Dgn (2D model)

    I've suggested elsewhere that a good approach is to attach your source DGN model as a reference to your destination DGN model.  Then copy from the reference model to the destination model.  That way, MicroStation has already taken care of many settings so you don't have to.

     
    Regards, Jon Summers
    LA Solutions

  • Hi Paolo,

    And then Flatten Curve.

    Isn't it simpler to export element to 2D file(s), so the flattening is handled by MicroStation automatically?

    My desire is ... using OpenDesignFileForProgram.

    Honestly, it's not your desired in my opinion. Your want to copy elements from one file to another. You choose to use DGN work file (OpenDesignFileForProgram), even when I mentioned in another discussion that this way is more limited and can be tricky. Can this way be use? I am not sure, probably yes, but based on your expected programming knowledge, why more complicated way was chosen?

    I read in the Microstation VBA Help that, with OpenDesignFileForProgram, is not possible make use of AddElement (is it correct?).

    I agree with Jon: When it's written in documentation, why to ask whether it's correct or not.

    Moreover, it shows not good knowledge of what work file is and how it works. Did you search for all discussions about issues found when OpenDesignFileForProgram is used? You should be aware that sometimes discussions about OpenDesignFileForProgram leads to information provided in MDL API documentation.

    Work file is opened at background, it's not opened as active design file for editing, so "to add elements" makes no sense. To use "element copy" is the right approach, as the file can be treated as a stream of elements, not encapsulated by completely initialized MicroStation engine.

    I tried to do a lot of attempts to solve those problems, without luck...

    I think your approach to the problem solution is wrong. Every developer, when entering new topic or area, has to start from the ground, represented by atomic operations. When one step works, let's make it more complex. When you are reporting 3 different problems at once, it means you want to do too much.

    My recommendation is (in addition to Jon's advice to use reference attachment) to start from e.g. one line, try to copy. Does it work? Fine, add CopyContext configuration. Does it still works? Let's do next step, try another data...

    I insert the last one here

    I do not understand the code.

    Why this command is used and what is expected it will do?

    ActiveDesignFile.Models("Default").AddElement myCellToExchange

    Also:

    myCellToExchange.Rewrite
    myCellToExchange.Redraw
    
    myDgn.RewriteLevels
    myDgn.Save
    myDgn.Close

    Why Redraw? In the file that is not displayed? Even in active model there is no reason to redraw element, only when displayed in dynamics.

    Why RewriteLevels? I am not sure, but I assume CopyElement and CopyContext manage levels automatically, at least it's stated in documentation.

    Regards,

      Jan

  • Prefer OpenDesignFile to OpenDesignFileForProgram! Read this blog.

    I'll do.

    Why should it not be correct?

    Of course, the help is correct! I meant that maybe I did't understand well NerdSlight smile

    I've suggested elsewhere that a good approach is to attach your source DGN model as a reference to your destination DGN model.  Then copy from the reference model to the destination model.  That way, MicroStation has already taken care of many settings so you don't have to.

    Yes, you are right. I did not followed that way because my original idea was to put each cell in an array and then do a cicle to copy each cell. 
    The code I posted is the last attempt to solve those problems: I tried to avoid using the array hoping problems were created by use the array.

    Moreover, in the source file, I create a model for each 3D object, each model has 6 levels, named:

    - sPipeID & " - 3D - Total", where I copy the 3D object (a pipe);

    - sPipeID & " - 3D - Axis", where I copy the 3D object, and I obtain the axis of the pipe, droping down to "simplest" element;

    - sPipeID & " - Isometric - Sketch", I do Visible Edge and flatten curve in Iso view

    - sPipeID & " - Top - Sketch", I do Visible Edge and flatten curve in top view

    - sPipeID & " - Left - Sketch", I do Visible Edge and flatten curve in Left view

    - sPipeID & " - Front - Sketch", I do Visible Edge and flatten curve in Front view

    But I cannot know how many models and levels my source file can originally has (over the ones I create), so it seemed easier don't follow your suggestion (also to experiment the use of OpenDesignFileForProgram (or OpenDesignFile, now). Of course I could do a cicle for each model in the attachment file, and a If statement instr(model.name, " - Top - Sketch") = true then addelement to target file....but It seems a little "intricate".

    1. What do you think, Jon? Would you suggest me to follow the attachment idea, anyway?

    2. With OpenDesignFile, can I use AddElement?

    Thanks a lot, I really appreciate  your help Relaxed

    Bye,

    P

  • Would you suggest me to follow the attachment idea, anyway?

    It's hard to say, because (at least for me) it's very hard to understand the situation and your workflows. To share one DGN example, e.g. a original file and expected, will save these hundreds words.

    Regards,

      Jan

  • Good evening Jan,

    Isn't it simpler to export element to 2D file(s), so the flattening is handled by MicroStation automatically?

    Probably yes, also because otherwise you wouldn't have asked ...

    Honestly, it's not your desired in my opinion.

    Thinking

    Your want to copy elements from one file to another.

    Surely true.

    ou choose to use DGN work file (OpenDesignFileForProgram), even when I mentioned in another discussion that this way is more limited and can be tricky. Can this way be use? I am not sure, probably yes, but based on your expected programming knowledge, why more complicated way was chosen?

    it is true what you say about the past discussion... but it is also true that you have kindly advised me to read up and read some books. In Learning MIcrostation VBA, OpenDesignFileForProgram is used in a "quite similar" example.

    So ODFFP seemed to me a viable solution, because I had an example of its use, it was an opportunity to learn how to use it (even if it was not successful) and in any case an opportunity for growth. Consider that I can only dedicate my (little) free time to VBA and on the other hand, I have a lot of urgency to develop this macro (I usually dine in front of the PC) and, also for this reason, the first solution seemed the good one Blush

    I agree with Jon: When it's written in documentation, why to ask whether it's correct or not.

    I clearly know that the help is correct: I just wanted to be reassured that I understood correctly.

    I think your approach to the problem solution is wrong. Every developer, when entering new topic or area, has to start from the ground, represented by atomic operations.

    Right. I did it. 

    My recommendation is (in addition to Jon's advice to use reference attachment) to start from e.g. one line, try to copy. Does it work? Fine, add CopyContext configuration. Does it still works? Let's do next step, try another data...

    I did it too. Remember I wrote that code was only one of several attempts.

    The macro has several steps (copy to 2D file is just the final one) and I started writing the first one (Get levels), following the code row by row in execution and testing with more than one file (with different situations). The same for the second one (Get CellElements), and so on...

    For this macro (and next ones) I did what you suggested): solved the problem on paper, read the book and the help (sometimes the net)

    ...and for the first time I got procedures (practically all of them)  really short. (Classes helped a lot in this aim)

    in addition to Jon's advice to use reference attachment

    I'll do, or at least, I'll try. ALso if I am a little sad: it seems a way to work around the problem. 
    Do you think the attachment solution is better than also OpenDesignFile?

    Why this command is used and what is expected it will do?

    To check if I got same problem ADDING elememnts in the SOURCE file. 

    Why Redraw? In the file that is not displayed? Even in active model there is no reason to redraw element, only when displayed in dynamics.

    Why RewriteLevels? I am not sure, but I assume CopyElement and CopyContext manage levels automatically, at least it's stated in documentation.

    JoyJoyYou are right. 

    They may seem absurd or curious instructions, in reality they were only the last, not credible and desperate attempts ... just to not give up! Joy

    Thanks a lot. 

    P

  • Hi Jon Summers,

    I've suggested elsewhere that a good approach is to attach your source DGN model as a reference to your destination DGN model.  Then copy from the reference model to the destination model.  That way, MicroStation has already taken care of many settings so you don't have to.

    How could I do that?

    Is it possible attach reference to a close dgn? 

    Please , remember my aim is to add elements in a destination dgn each of them in defined position.

    Thanks a lot.

    Bye,

    PM

  • Is it possible attach reference to a close dgn? 

    You can't do anything with a closed file, whether it's a DGN file, Word file or something else.  There's no magic in a DGN file...

    • A file must be open for read in order to view its contents. 
    • A file must be open for write in order to modify its contents.
    Is it possible attach reference to a close dgn? My aim is to add elements in a destination dgn each of them in defined position.

    I suggest that you first try this manually: attach the DGN model, with elements to be copied, to your active model.  Then use MicroStation's Copy tool to copy from the reference into the active DGN model.  Note that the Copy command works when a fence is active.   Practise as a MicroStation user will inform your coding as a MicroStation developer.

     
    Regards, Jon Summers
    LA Solutions

  • You can't do anything with a closed file, whether it's a DGN file, Word file or something else.  There's no magic in a DGN file.  A file must be open for read in order to view its contents.  A file must be open for write in order to modify its contents

    Ok, but we come back to the problem: I don't want to really open the destination file (OpenDesignFile): I wish user don't see anything...

  • I wish user don't see anything

    VBA developers often state something similar.  The real question is, "Do your users care that something is happening automatically?"

    First, get your app working.  Second, ask users for comments.  If a user tells you:  "I  don't want to see MicroStation while this automatic process continues!" then is the time to figure out how to make MicroStation invisible.

     
    Regards, Jon Summers
    LA Solutions

  • It is true Jin, but...
    ...in this case I am developer and user too. :-)

    Anyway, I should open the file with OpenDesignFIle, attach a reference and use the CopyElement or AddElement (which one?), then detach the file, right?