[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

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

  • 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

  • Attaching the source file as a reference allows MicroStation' internal processing to manage a lot of settings for you, depending on the options you choose for the attachment. The True Scale option will automatically handle any scaling from mismatched units (such as feet in the source and meters in the destination) without you having to test for units and resolution and conversion factors to make sure that your elements come in at the right size. You CAN do all that coding to try to figure it out for every possible case, or you can let the program do that for you. There are other reference settings that you can set with an option during attachment, without having to write a lot of extra code.

    This isn't exactly about programming but I have a CAD tech who was trying to copy elements between files, but he was using the Windows Cut/Copy/Paste to do so, and he wasn't always getting the results he thought he should (units, rotations, other things). I taught him to attach the reference and copy from that, and he was so happy to see how well it worked.

    Jon and Jan are very, very smart, and highly educated about programming. I know they've never steered me wrong!

    MaryB

    Power GeoPak 08.11.09.918
    Power InRoads 08.11.09.918
    OpenRoads Designer 2021 R2

        

Reply
  • Attaching the source file as a reference allows MicroStation' internal processing to manage a lot of settings for you, depending on the options you choose for the attachment. The True Scale option will automatically handle any scaling from mismatched units (such as feet in the source and meters in the destination) without you having to test for units and resolution and conversion factors to make sure that your elements come in at the right size. You CAN do all that coding to try to figure it out for every possible case, or you can let the program do that for you. There are other reference settings that you can set with an option during attachment, without having to write a lot of extra code.

    This isn't exactly about programming but I have a CAD tech who was trying to copy elements between files, but he was using the Windows Cut/Copy/Paste to do so, and he wasn't always getting the results he thought he should (units, rotations, other things). I taught him to attach the reference and copy from that, and he was so happy to see how well it worked.

    Jon and Jan are very, very smart, and highly educated about programming. I know they've never steered me wrong!

    MaryB

    Power GeoPak 08.11.09.918
    Power InRoads 08.11.09.918
    OpenRoads Designer 2021 R2

        

Children
No Data