New Sketch

New Sketch

Anonymous
Not applicable
423 Views
7 Replies
Message 1 of 8

New Sketch

Anonymous
Not applicable
I took the example code from the help file and tried to build the
equivalent of a bit of code to select a face and create a new sketch
on it. When I run in debug mode and set a break point on the event
stop line, everything is fine. When I just let it run, it crashes
Inventor. Does anyone have a sample piece of code which doesn't crash
which I could use as an example for doing the equivalent of
interactively selecting a face and creating a new sketch on it?
0 Likes
424 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Charles

I don't have any code, but what example file are you using? Can you post the offending
snip of code?

--
Kent


"Charles Bliss" wrote in message news:3C0663A5.5A820F17@cbliss.com...
> I took the example code from the help file and tried to build the
> equivalent of a bit of code to select a face and create a new sketch
> on it. When I run in debug mode and set a break point on the event
> stop line, everything is fine. When I just let it run, it crashes
> Inventor. Does anyone have a sample piece of code which doesn't crash
> which I could use as an example for doing the equivalent of
> interactively selecting a face and creating a new sketch on it?
>
0 Likes
Message 3 of 8

Anonymous
Not applicable
I can send you an example tomorrow -- currently I am at home and have no
Inventor with me.
That example is making the slot of a cupboard, i.e. a hole in the front-side
of the panel and one constraint to the first one at the head-side of the
panel.
It ask the user to select three sides, front, head and side (in that order),
because this let me know at which corner the slot has to be build. The
problem faced with this program is to determine the X and Y directions into
the "wood" otherwise the hole definition would be build in open air. Another
challenge solved is the use of constraints and projections from one face
into the other. Also building an dimension line really connected to the
origin of the sketch.
Finally what I discovered was that you can traverse a part when selected
into faces, edges etc. However when you referenced such a face from these
lists, and then build a hole in that face, the list is not valid anymore
because the surface is changed, increasing the number of edges for example.
Currently I overcome this problem (it's a problem because I cannot find my
previous selected faces again), by delaying the creation of holes until all
my sketches are build. Then I don't need the faces anymore.
So if you are a bit patient this example give you some head start.
Anton
0 Likes
Message 4 of 8

Anonymous
Not applicable
Charles,

I tried the simple way with the CommandManager to start a Inventor command
like creating a new sketch, but it seems to out of order, when I try it, it
returns a error, the Export to SAT Example works fine though!!

This it what I tried:

Call ThisApplication.CommandManager.StartCommand(kCreateSketchCommand)

PS. Anton I would be happy to see your code or just the part of the code to
select if possible.

-Patrick
0 Likes
Message 5 of 8

Anonymous
Not applicable
Hi Charles

Here is how we handle that in our XLS Import Add-In.
Note that the user can select either a face or a work plane.

----------------------------------------------------------------------------
---------
First in a module, we declare:

' collection of selection filters
Public colFilters As Collection

----------------------------------------------------------------------------
---------
Then in the code itself we have:

RePickFace:

' Create a new clsSelect object.
Dim oSelect As New clsSelect

' Call the pick method of the clsSelect object and set
' the filter to pick any face or work plane.
Dim oFace As Face
Dim oSelObject As Object

' Initialize the filter collection
Set colFilters = New Collection
' Add two filters to collection, selected entities can be of type:
' - planar face
' - work plane
colFilters.Add kPartFacePlanarFilter
colFilters.Add kWorkPlaneFilter

' the selected element is attributed to oSelObject
' it can be either a planar face, or a work plane: 2 different types
' that is why oSelObject is of type Object (and not Face...)
Set oSelObject = oSelect.Pick(colFilters)

' Check to make sure an object was selected.
If Not oSelObject Is Nothing Then

' Check if the selected object is a Face
If TypeOf oSelObject Is Inventor.Face Then

Set oFace = oSelObject
' Check if the face is planar
If oFace.SurfaceType = kPlaneSurface Then
' Create a new sketch on this surface
' parameter True = retrieve face's contour
Set oSketch = oCompDef.Sketches.Add(oFace, True)
Else
' if selected face is not planar, return to selection
Call MsgBox("A planar face or work plane must be selected.")
GoTo RePickFace
End If

' The selected object is not a Face, check if it is a Work Plane
ElseIf TypeOf oSelObject Is Inventor.WorkPlane Then

' Create a new sketch on this work plane
Set oSketch = oCompDef.Sketches.Add(oSelObject)

Else

' if selected element is neither a Face nor a work plane
' return to selection
Call MsgBox("A planar face or work plane must be selected.")
GoTo RePickFace

End If

End If

----------------------------------------------------------------------------
---------
Finally, in the clsSelect class we changed the declaration to be:

Public Function Pick(filters As Collection) As Object
...

And the filter part is now:

' Set the filter using the value passed in.
' Loop through the collection off filters and add each one to the
selectevent filters
Dim filter As SelectionFilterEnum
Dim iFor As Integer
For iFor = 1 To filters.Count
filter = filters.Item(iFor)
oSelectEvents.AddSelectionFilter filter
Next

Hope this helps

M&X

"Charles Bliss" a écrit dans le message news:
3C0663A5.5A820F17@cbliss.com...
> I took the example code from the help file and tried to build the
> equivalent of a bit of code to select a face and create a new sketch
> on it. When I run in debug mode and set a break point on the event
> stop line, everything is fine. When I just let it run, it crashes
> Inventor. Does anyone have a sample piece of code which doesn't crash
> which I could use as an example for doing the equivalent of
> interactively selecting a face and creating a new sketch on it?
>
0 Likes
Message 6 of 8

Anonymous
Not applicable
So, what I am hearing is that it is only crashing for me. Both you and Patrick
have not suggested any problems with clsSelect per say yet when I run the code a
second time to set a second sketch, it crashes.

I have posted a trimmed back version of the clsSelect and a form module to
Customer Files. If I run it, it will either crash on the first or second face
select. Any help would be greatly appreciated.

M&X wrote:

> Hi Charles
>
> Here is how we handle that in our XLS Import Add-In.
> Note that the user can select either a face or a work plane.
>
> ----------------------------------------------------------------------------
> ---------
> First in a module, we declare:
>
> ' collection of selection filters
> Public colFilters As Collection
>
> ----------------------------------------------------------------------------
> ---------
> Then in the code itself we have:
>
> RePickFace:
>
> ' Create a new clsSelect object.
> Dim oSelect As New clsSelect
>
> ' Call the pick method of the clsSelect object and set
> ' the filter to pick any face or work plane.
> Dim oFace As Face
> Dim oSelObject As Object
>
> ' Initialize the filter collection
> Set colFilters = New Collection
> ' Add two filters to collection, selected entities can be of type:
> ' - planar face
> ' - work plane
> colFilters.Add kPartFacePlanarFilter
> colFilters.Add kWorkPlaneFilter
>
> ' the selected element is attributed to oSelObject
> ' it can be either a planar face, or a work plane: 2 different types
> ' that is why oSelObject is of type Object (and not Face...)
> Set oSelObject = oSelect.Pick(colFilters)
>
> ' Check to make sure an object was selected.
> If Not oSelObject Is Nothing Then
>
> ' Check if the selected object is a Face
> If TypeOf oSelObject Is Inventor.Face Then
>
> Set oFace = oSelObject
> ' Check if the face is planar
> If oFace.SurfaceType = kPlaneSurface Then
> ' Create a new sketch on this surface
> ' parameter True = retrieve face's contour
> Set oSketch = oCompDef.Sketches.Add(oFace, True)
> Else
> ' if selected face is not planar, return to selection
> Call MsgBox("A planar face or work plane must be selected.")
> GoTo RePickFace
> End If
>
> ' The selected object is not a Face, check if it is a Work Plane
> ElseIf TypeOf oSelObject Is Inventor.WorkPlane Then
>
> ' Create a new sketch on this work plane
> Set oSketch = oCompDef.Sketches.Add(oSelObject)
>
> Else
>
> ' if selected element is neither a Face nor a work plane
> ' return to selection
> Call MsgBox("A planar face or work plane must be selected.")
> GoTo RePickFace
>
> End If
>
> End If
>
> ----------------------------------------------------------------------------
> ---------
> Finally, in the clsSelect class we changed the declaration to be:
>
> Public Function Pick(filters As Collection) As Object
> ...
>
> And the filter part is now:
>
> ' Set the filter using the value passed in.
> ' Loop through the collection off filters and add each one to the
> selectevent filters
> Dim filter As SelectionFilterEnum
> Dim iFor As Integer
> For iFor = 1 To filters.Count
> filter = filters.Item(iFor)
> oSelectEvents.AddSelectionFilter filter
> Next
>
> Hope this helps
>
> M&X
>
> "Charles Bliss" a écrit dans le message news:
> 3C0663A5.5A820F17@cbliss.com...
> > I took the example code from the help file and tried to build the
> > equivalent of a bit of code to select a face and create a new sketch
> > on it. When I run in debug mode and set a break point on the event
> > stop line, everything is fine. When I just let it run, it crashes
> > Inventor. Does anyone have a sample piece of code which doesn't crash
> > which I could use as an example for doing the equivalent of
> > interactively selecting a face and creating a new sketch on it?
> >
0 Likes
Message 7 of 8

Anonymous
Not applicable
I've had a look at your code... And got the same problem! (sorry)

However, there is a big difference between what you do here and what I have
done since now with the selection stuff: I have never done it in an external
application till today.
I've only used selections within add-ins and macros.

While playing around with you code, I found out that if I call a MsgBox
somewhere between the Loop (Do While bStillSelecting...) and the
oInteractEvents.Stop I do not have any crash any more.
My guess is that VB (or Inventor, or the API, or Windows, or all of them???)
needs a kind of a break inbetween (but please, don't ask me why). I tried
with a break and it worked... Except that my window does not come back in
front (but I reckon that you know what to do with your SetForegroundWindow).

So here is what I end up with:

...
Do While bStillSelecting
DoEvents
Loop
'MsgBox "Thank you for selecting a Face!"

' Get the selected item. If more than one thing was selected,
' just get the first item and ignore the rest.
Dim oSelectedEnts As ObjectsEnumerator
Set oSelectedEnts = oSelectEvents.SelectedEntities

If oSelectedEnts.Count > 0 Then
Set Pick = oSelectedEnts.Item(1)
Else
Set Pick = Nothing
End If

Dim PauseTime, Start

PauseTime = 0.5 ' Define the how long the pause will be.
Start = Timer ' start time.

Do While Timer < Start + PauseTime
DoEvents ' Give control to other processes.
Loop

' Stop the InteractionEvents object.
oInteractEvents.Stop
...

This works with me. Hope it does with you too.

M(&X)

"Charles Bliss" a écrit dans le message news:
3C07A8B9.7A60BDAE@cbliss.com...
> So, what I am hearing is that it is only crashing for me. Both you and
Patrick
> have not suggested any problems with clsSelect per say yet when I run the
code a
> second time to set a second sketch, it crashes.
>
> I have posted a trimmed back version of the clsSelect and a form module to
> Customer Files. If I run it, it will either crash on the first or second
face
> select. Any help would be greatly appreciated.
>
0 Likes
Message 8 of 8

Anonymous
Not applicable
Quite by accident, I happened to meet someone who had developed portions of the
API. I asked him to look over the code. He and I spent several hours on it and
he recognized that there is a problem running this out of process but couldn't
figure out what it is. I guess that there is a subtle "defect", at least that
is the conclusion we came to. Since then I have piled so much other code into
the program that the crash is less important right now. Ultimately, I will
create it "in process" as an Add-In.

M&X wrote:

> I've had a look at your code... And got the same problem! (sorry)
>
> However, there is a big difference between what you do here and what I have
> done since now with the selection stuff: I have never done it in an external
> application till today.
> I've only used selections within add-ins and macros.
>
> While playing around with you code, I found out that if I call a MsgBox
> somewhere between the Loop (Do While bStillSelecting...) and the
> oInteractEvents.Stop I do not have any crash any more.
> My guess is that VB (or Inventor, or the API, or Windows, or all of them???)
> needs a kind of a break inbetween (but please, don't ask me why). I tried
> with a break and it worked... Except that my window does not come back in
> front (but I reckon that you know what to do with your SetForegroundWindow).
>
> So here is what I end up with:
>
> ...
> Do While bStillSelecting
> DoEvents
> Loop
> 'MsgBox "Thank you for selecting a Face!"
>
> ' Get the selected item. If more than one thing was selected,
> ' just get the first item and ignore the rest.
> Dim oSelectedEnts As ObjectsEnumerator
> Set oSelectedEnts = oSelectEvents.SelectedEntities
>
> If oSelectedEnts.Count > 0 Then
> Set Pick = oSelectedEnts.Item(1)
> Else
> Set Pick = Nothing
> End If
>
> Dim PauseTime, Start
>
> PauseTime = 0.5 ' Define the how long the pause will be.
> Start = Timer ' start time.
>
> Do While Timer < Start + PauseTime
> DoEvents ' Give control to other processes.
> Loop
>
> ' Stop the InteractionEvents object.
> oInteractEvents.Stop
> ...
>
> This works with me. Hope it does with you too.
>
> M(&X)
>
> "Charles Bliss" a écrit dans le message news:
> 3C07A8B9.7A60BDAE@cbliss.com...
> > So, what I am hearing is that it is only crashing for me. Both you and
> Patrick
> > have not suggested any problems with clsSelect per say yet when I run the
> code a
> > second time to set a second sketch, it crashes.
> >
> > I have posted a trimmed back version of the clsSelect and a form module to
> > Customer Files. If I run it, it will either crash on the first or second
> face
> > select. Any help would be greatly appreciated.
> >
0 Likes