Author Topic: Geometric Constraints  (Read 413 times)

0 Members and 1 Guest are viewing this topic.

nekitip

  • Mosquito
  • Posts: 14
Geometric Constraints
« on: March 24, 2017, 01:04:25 pm »
I was trying to do a simple thing: insert text or block fixed to the midpoint of curve, horizontal or coplanar. That is possible in AutoCAD without problem, but how to do it programatically?
The only thing "close" to it in entire two hours of googleing was an old post here, written by Jeff H https://www.theswamp.org/index.php?topic=38418.0 I've couldn't find anything else unless it's objarx something...
I've really tried to understand what it does, but I got lost in entire concep...
Any help?

EDIT:
also, running code from that post is not going without problems - cad complaines that "one or more objects in this drawing cannot be saved to the specified format...", both on autosave and on save
« Last Edit: March 24, 2017, 02:47:16 pm by nekitip »

nekitip

  • Mosquito
  • Posts: 14
Re: Geometric Constraints
« Reply #1 on: March 26, 2017, 04:05:41 pm »
It took me a while to dig this out, but gold is found is here!
https://forums.autodesk.com/autodesk/attachments/autodesk/152/24647/1/Parametric%20VB%20Net%20Code.txt

Plenty of golden code that does compile without error. However, I still have no idea how to align text to curve and just guessing and trying blindely at the moment...

Atook

  • Swamp Rat
  • Posts: 829
Re: Geometric Constraints
« Reply #2 on: March 26, 2017, 06:29:03 pm »
Try something like this for your text rotation:
Code - C#: [Select]
  1. // rotation
  2. Vector3d deriv = Pipe.Polyline.GetFirstDerivative(insParam);
  3. rotation = deriv.RadsFromXAxis();
  4.  
  5. /// <summary>
  6. /// Returns the angle in rads from the x-axis that is used for the rotation of a block aligned with the vector
  7. /// </summary>
  8. /// <param name="vec"></param>
  9. /// <returns></returns>
  10. public static double RadsFromXAxis(this Vector3d vec)
  11. {
  12.  Vector3d xAxis = new Vector3d(1, 0, 0);
  13.  return Math.Atan2(vec.Y - xAxis.Y, vec.X - xAxis.X);
  14. }
  15.  

The RadsFromXAxis is a polyline extension found in Gile's GeometryExtensions. I can't find the download right now, but I'm pretty sure I picked it up here at the swamp.

Search for those and find them, some real gems from learning there.
« Last Edit: March 27, 2017, 11:39:22 am by Atook »

nekitip

  • Mosquito
  • Posts: 14
Re: Geometric Constraints
« Reply #3 on: March 27, 2017, 03:10:00 am »
I'm glad you're interested. However, I'm not sure if manual rotations are what I'm after.
The thing I'm talking about is a way to make CAD automaticaly align and rotate entites based on a rules you define.
This is really powefull tool, however, this link I provided (and refered to as gold) is the only other (beside Jeffs) available online!

There is also another link to blog of how to apply blocks to each polyline vertice, but I'm unable to find it. I literally opened hundreds of pages this weekend, trying to find something, so It's kind of lost in browser history.

Also, not directly related, but interesting:
http://adndevblog.typepad.com/autocad/2012/03/working-with-associative-parameters-from-api.html

The link I provided is a complete enough, however, I have no idea of what goes where. For example, line has edges - what text has? How to find point of interest for text?...

I think Jeff H could help maybe...

nekitip

  • Mosquito
  • Posts: 14
Re: Geometric Constraints
« Reply #4 on: March 27, 2017, 09:42:14 am »
finally, five frustrating hours later and more then 50 trial and error attempts, some success!
so, the most important thing is: whereas a line has edge; dbpoint, text and block have vertex! Once you realise that, it's 100 times easier, and you'll be able to spot patterns in code

then, you should only safely use this sub:
Code - vb.net: [Select]
  1. Public Sub SetEntCoincidentConstraint(ByVal curve1 As Curve, ByVal ent1 As Entity, ByVal curveEnd1 As CurvePoint, ByVal entType As SubentityType, ByVal trans As Transaction)
  2.            Using trans2 As Transaction = trans.TransactionManager.StartTransaction 'work with sub transactions in order to allow 1 undo that undoes it all.
  3.                Dim SubEntEdgePath1 As FullSubentityPath 'logical path to the edge (one edge object pretty simple)
  4.                Dim SubEntEdgePath2 As FullSubentityPath
  5.                Dim subEntPointPath1 As FullSubentityPath 'logical path to the user selected point (usually start, end, or mid)
  6.                Dim subEntPointPath2 As FullSubentityPath
  7.                'create a logical path from the object to its sub entities
  8.                CreateSubEntityPath(curve1, curveEnd1, SubEntEdgePath1, subEntPointPath1, trans2)
  9.                SubEntEdgePath2 = CreateSubEntityPath(ent1, entType)   'cutting point
  10.                subEntPointPath2 = CreateSubEntityPath(ent1, entType)  'point where
  11.  
  12.                'get the constraint group that exists on the same plane as our points (in this case just wcs + elevation, but if you get more advanced, this may need to be modified big time)
  13.                Dim consGrpId As ObjectId = GetConstraintGroup(True, trans2)
  14.                'use the logical paths to create a constraint between 2 selected points(CurvePoint) of the 2 supplied edges(curves)
  15.                Dim consGeomEdge1 As ConstrainedGeometry = Nothing
  16.                Dim consGeomEdge2 As ConstrainedGeometry = Nothing
  17.                Using constGrp As Assoc2dConstraintGroup = DirectCast(trans2.GetObject(consGrpId, OpenMode.ForWrite, False), Assoc2dConstraintGroup)
  18.                    Try
  19.                        consGeomEdge1 = AddConstrainedGeometry(constGrp, SubEntEdgePath1)
  20.                    Catch ex As Exception
  21.                        'aready there move on
  22.                    End Try
  23.                    Try
  24.                        consGeomEdge2 = AddConstrainedGeometry(constGrp, SubEntEdgePath2)
  25.                    Catch ex As Exception
  26.                        'aready there move on
  27.                    End Try
  28.                    Dim paths As FullSubentityPath() = {subEntPointPath1, subEntPointPath2}
  29.                    Dim newConstraint As GeometricalConstraint = constGrp.AddGeometricalConstraint(GeometricalConstraint.ConstraintType.Coincident, paths)
  30.                End Using
  31.                trans2.Commit()
  32.            End Using
  33.        End Sub
you will be able to add block or text to stick to midpoint of curve
So now it is possible to do some of the things, but I'm still unable to actually align text or block... how to do that?

Jeff H

  • Needs a day job
  • Posts: 5804
Re: Geometric Constraints
« Reply #5 on: March 29, 2017, 03:14:58 pm »
I'm glad you're interested. However, I'm not sure if manual rotations are what I'm after.
The thing I'm talking about is a way to make CAD automaticaly align and rotate entites based on a rules you define.
This is really powefull tool, however, this link I provided (and refered to as gold) is the only other (beside Jeffs) available online!

There is also another link to blog of how to apply blocks to each polyline vertice, but I'm unable to find it. I literally opened hundreds of pages this weekend, trying to find something, so It's kind of lost in browser history.

Also, not directly related, but interesting:
http://adndevblog.typepad.com/autocad/2012/03/working-with-associative-parameters-from-api.html

The link I provided is a complete enough, however, I have no idea of what goes where. For example, line has edges - what text has? How to find point of interest for text?...

I think Jeff H could help maybe...
I can not find that AU class Stephen Preston did on this subject. Maybe reach out to him to see if he could get handout from that that class.