Hi Guys,
I'm in the middle of transferring to Microstation Connect Version 13, and coming from Microstation V8i (Select series 4). Very busy transferring al the VBA from old to new, but running in to a problem. I have a Function to fill in the textblock in the drawing, which works perfect on V8i, but gives me a type mismatch on Connect, on this line :
Set ele = Pos.GetElementByID(idProjectnummer)
Complete function is :
Public Function WelkeID() As String Dim ele As TextElement Dim idProjectnummer As DLong Dim Pos As ModelReference idProjectnummer = DLongFromString("25428") 'On Error GoTo Foutje Set Pos = ActiveDesignFile.Models("Default") Set ele = Pos.GetElementByID(idProjectnummer) WelkeID = "Nieuw" Set ele = Nothing Exit Function Foutje: WelkeID = "Oud" Set ele = Nothing End Function
Is this way of programming no longer valid in Microstation Connect?
Thanks already for the help,
Leo.
Hi Leo,
similarly to Jon I have also a few comments.
Leo van der Hoeven said:Complete function is :
Honestly, in my opinion the function is badly written. When I reformat it to something that makes more sense to me and add comments:
Public Function WelkeID() As String 'On Error GoTo Foutje ' Error is generated here only when code is wrong, so why to use OnError? Dim idProjectnummer As DLong idProjectnummer = DLongFromString("25428") ' Why id created from string? ' Do you know the element with ID 25428 exists? Dim Pos As ModelReference Set Pos = ActiveDesignFile.Models("Default") ' Are you sure a mode "Default" exists? Dim ele As TextElement Set ele = Pos.GetElementByID(idProjectnummer) ' How you can know that element is text? WelkeID = "Nieuw" Exit Function
But it still does not answer my main question: What is an intention of the function? To find whether element id 25428 exists in default model?
Leo van der Hoeven said:but gives me a type mismatch on Connect, on this line :
It's why I asked in the code above whether you are sure the element exists and when it exists, that it's text.
Both ideas are wrong in my case: Element should never be identified explicitly by its id (something else is to obtain id dynamically using e.g. scanning) and without further test code, it's risky to expect element type (assign element o TextElement variable).
Leo van der Hoeven said:Is this way of programming no longer valid in Microstation Connect?
My experience and how VBA API was migrated to CE is similar to what Jon wrote: Typically VBA code runs without modifications. But I would like to change it to that typically good VBA code runs fine. Problems appears often when code written incorrectly, running fine in V8, is ported to CE, because something tiny has changed in API or inside MicroStation engine.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Hi Jan and Jon,
I' m quite aware of the possibility the code is written a bit poorly, but I can't take the credit for that, since it's made by one of my predecessors for op to 15 years ago. But al that time it worked.
When I go through the code it code it does seems overly complicated, so I do plan to optimize the code, but for now my priority is to get it working.
I'll try to explain what the code does, or should do.
This code is used to create a new drawing file from a seed file.
The seed file contains two models, one is Default, the other DefaultPosities. We have an Userform for the user to enter the text that has to go in the Title block. which is Projectnumber (idProjectnummer) project name , client name and Calculation Number, User and date, when a Project is started.User fills in project number, name, client name, and calculation number, in a textbox of the userform, after a button press there are subs that check if file already exists and such.
Then the code arrived at the above part. This part should check if we are dealing with a New project ( "Nieuw" ), Which is when "25428"is found, or an existing ( "Oud" ) When "25428"isn't found.
When it's a new one Variable "WelkeID" is set to NEW. when not new to old. I have no Idea why this is done with an error, and also find this rather sloppy.
Project always contains one or more positions, which have a Name and position number. These are filled in on another userform, and stored in model DefaultPosities. But this is al done further along in the code.
When I comment out the line " Set ele = Pos.GetElementByID(idProjectnummer) " I get the same error in the next sub, where the actual data should be entered to the title block
I put a watch on the "idprojectnummer", see screenshot:
I checked the ID numbers, they are all existing and correct, Model Default exists, and there is only text to insert.
The sub the error is in now:
Public Sub WijzigDefaultTitelbalk() Dim ele As TextElement Dim idProjectnummer As DLong Dim idCalculatienummer As DLong Dim idOpdrachtgever As DLong Dim idProject As DLong Dim model As ModelReference Set model = ActiveDesignFile.Models("Default") If WelkeID() = "Nieuw" Then idProjectnummer = DLongFromString("25428") idCalculatienummer = DLongFromString("25429") idOpdrachtgever = DLongFromString("25425") idProject = DLongFromString("25424") idBenaming = DLongFromString("25401") idPositienummer = DLongFromString("25430") Else idOpdrachtgever = DLongFromString("325105") idBenaming = DLongFromString("325104") idProject = DLongFromString("325103") idProjectnummer = DLongFromString("325106") idCalculatienummer = DLongFromString("325107") idPositienummer = DLongFromString("325100") End If If frmNieuwProject.txtProject.TextLength >= 9 Then If frmNieuwProject.txtCalculatie.TextLength = 10 Then 'was 9 140225 Set ele = model.GetElementByID(idProjectnummer) ele.IsLocked = False ele.Text = frmNieuwProject.txtProject.Value ele.Rewrite ele.IsLocked = True Set ele = Nothing Set ele = model.GetElementByID(idCalculatienummer) ele.IsLocked = False ele.Text = frmNieuwProject.txtCalculatie.Value ele.Rewrite ele.IsLocked = True Set ele = Nothing Set ele = model.GetElementByID(idOpdrachtgever) ele.IsLocked = False ele.Text = frmNieuwProject.txtOpdrachtgever.Value ele.Rewrite ele.IsLocked = True Set ele = Nothing Set ele = model.GetElementByID(idProject) ele.IsLocked = False ele.Text = frmNieuwProject.txtOmschrijving.Value ele.Rewrite ele.IsLocked = True Set ele = Nothing End If End If End Sub
the error now is on Line 30. So what can this be?
Thanks Leo
Leo van der Hoeven said:the error now is on Line 30Set ele = model.GetElementByID(idProjectnummer)
Set ele = model.GetElementByID(idProjectnummer)
That element ID is invalid in that DGN model. We can't see which model is the active model. Add some debug statements...
Debug.Print "Active model: " & model.Name Debug.Print "Get element from ID " & DLongToString (idProjectnummer) Set ele = model.GetElementByID(idProjectnummer) Debug.Assert Not ele Is Nothing
Regards, Jon Summers LA Solutions
Hi Jon,
I entered the debug statements as requested.
Immediate windows gives back:
Active model: DefaultGet element from ID 25428
Looks good to me.
The debug.Assert line never runs, since the error happens in the Set ele line.
Thanks,
Leo
Leo van der Hoeven said:Get element from ID 25428
What type of element is ID 25428?
Can you change ele to be Element (not TextElement) and to debug what is its type?