How to apply a scale to a sketch ?

How to apply a scale to a sketch ?

TONELLAL
Collaborator Collaborator
1,450 Views
6 Replies
Message 1 of 7

How to apply a scale to a sketch ?

TONELLAL
Collaborator
Collaborator

Hello,

I can move or rotate a sketch, using MoveSketchObjects(objects, vector) or RotateSketchObjects(objects, center,  angle). But what about Scaling ? I can't find nothing like Scaleobjects(objects, basepoint, scale) or using a matrix, like TransformBy (matrix).

I think it is possible, but where ?

0 Likes
Accepted solutions (1)
1,451 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Good point. I have never used it, so I never thought to look for it within iLogic.

After searching for it myself, and not finding it, I would assume that you would have to invoke this command using the ThisApplication.CommandManager.ControlDefinitions.Item("SketchScaleCmd").Activate

 

You could try searching in the Inventor Ideas forum to see if anyone else has posted an idea about adding this functionality in iLogic & VBA.  If there are none, you could try posting a new idea for this.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

TONELLAL
Collaborator
Collaborator

I didn't bring it up again because I found several (old) messages, some from Autodesk, telling it was very complex and so not implemented.

I found something in VBA, using a transform matrix like :

(XScale           0)

(     0      YScale)

This work to move all points in a drawing sketch, so :

-for lines it's ok

-for circles it move the center, just modify the radius after.

-for arcs it move start, center and end points. The problem is when one of these points move, the other too... There is a function "oSketch.SketchArcs.Item(1).Geometry.GetArcData" and "oSketch.SketchArcs.Item(1).Geometry.PutArcData", but I can't get it to work. I can get the datas (center, radius, start point, sweep angle), edit them, but I can't send back the new datas... any idea welcome ^^.

-for other items : not tested...

 

Here's the code, to test it create a drawing with a sketch containing only lines. The sketch will be sketched with a factor of 0.5 from the origin. (This code only work with lines)

 

Sub test_geo()

 

Dim oDoc As DrawingDocument

Dim oSketches As DrawingSketches

Dim oSketch As DrawingSketch

Dim oTG As TransientGeometry

Dim oSketchPoints As SketchPoints

Dim oSketchPoint As SketchPoint

Dim oScaleMatrix As Matrix2d

 

Set oDoc = ThisApplication.ActiveDocument

Set oSketches = oDoc.ActiveSheet.Sketches

 

Set oTG = ThisApplication.TransientGeometry

 

'Define the matrix

Set oScaleMatrix = oTG.CreateMatrix2d

oScaleMatrix.Cell(1, 1) = 0.5

oScaleMatrix.Cell(1, 2) = 0

oScaleMatrix.Cell(2, 1) = 0

oScaleMatrix.Cell(2, 2) = 0.5

 

'Select the first sketch, select points and edit the sketch

Set oSketch = oSketches(1)

Set oSketchPoints = oSketch.SketchPoints

oSketch.Edit

 

'For each point, apply the matrix to transform it then move it to the new coordinates

For Each oSketchPoint In oSketchPoints

    Set p = oTG.CreatePoint2d(oSketchPoint.Geometry.X, oSketchPoint.Geometry.Y)

    Call p.TransformBy(oScaleMatrix)

    Call oSketchPoint.MoveTo(p)

Next oSketchPoint

 

oSketch.ExitEdit

 

End Sub

0 Likes
Message 4 of 7

J-Camper
Advisor
Advisor

I don't think that code is truly scaling the way you want it:

Before ScalingBefore ScalingAfter ScalingAfter Scaling

With that being said, the code does modify circles and arcs in my testing:

Before Scaling [with arc/circle]Before Scaling [with arc/circle]After Scaling [with arc/circle]After Scaling [with arc/circle]

If the sketch is in a drawing, can you make it a sketch symbol?  Those are easy to scale.

0 Likes
Message 5 of 7

TONELLAL
Collaborator
Collaborator

Thanks for your feedback ! You are right, on your sketch scale is not correct, but circles and arcs are correctly modified...

In fact there are several things :

-a rectangle, is made of 4 lines, each one with geometric constraint(parallel or horizontal/vertical). Du e to these constraints, when a point move, an other point move too.Finally, each point move twice. If you delete cnstraints on lines it will be ok.

This can be avoid by storing all points coordinates before modifying, then move a point only if its current coordinates are different from the originals.

-for the circles and arcs : they are not concerned by the code, but they are constrained on lines. So when a line is modified, they are modified too.

 

Your proposition about using sketch symbol is very interesting. I can from Inventor copy/paste objects from a drawing sketch to a sketch symbol. Do you know how I can do the same thing using VBA ?

0 Likes
Message 6 of 7

J-Camper
Advisor
Advisor
Accepted solution

I think this should work to create SketchedSymbol from a Sketch, written as an external rule:

Sub Main
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then MessageBox.Show("Run in Drawing Document", "Document Type Error") : Exit Sub
Dim drwDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim aSketch As DrawingSketch = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchObjectFilter, "Select a Sketch")
If IsNothing(aSketch) Then MessageBox.Show("Nothing was selected.", "Selection Error") : Exit Sub
Dim aSymbolDef As SketchedSymbolDefinition
Dim NewSymbolName As String 
10 :
NewSymbolName = InputBox("Please input name of new sketch symbol:", "New Symbol Name", "New Symbol")
Try
	aSymbolDef = drwDoc.SketchedSymbolDefinitions.Add(NewSymbolName)
Catch
	If MessageBox.Show(NewSymbolName & " is already in use. Try a different name?", "Name is Used.",MessageBoxButtons.YesNo) = vbYes Then GoTo 10 Else Exit Sub
End Try
Dim newSketch As DrawingSketch
aSymbolDef.Edit(newSketch)
aSketch.CopyContentsTo(newSketch)
aSymbolDef.ExitEdit(True)
End Sub

I do want to mention that dimensions cannot be pulled on sketch symbols.  I'm not sure what you are using this for, but I hope that is okay.  If you do need to dimension it, another possibility would be to set up a "Draft" sketch with has scaling options with it.

Message 7 of 7

TONELLAL
Collaborator
Collaborator

The final goal is to modify a drawing from for example A1 to A3, keepeing the same appearance (I know that I can print modifying sheet size, but some customers can have some... strange requests ^^). So this mean moving and scaling all objects on the sheet. I can modify views, texts, balloons, partlists, etc, the only thing left was Drawing Sketches. I have adapted your code to transform a Drawing sketch to a Symbol, then insert it to the new coordinates with a new scale.

0 Likes