generateCollisionShapes() doesn't seem to work - arkit

When applying generateCollisionShapes() on the root of the following hierarchy nothing happens.
The 'collision' component of the root entity is still nil.
The root entity class looks like this:
class SomeEntity: Entity, HasAnchoring, HasModel, HasCollision
The child entity is loaded from .usdz file, and its hierarchy is:
Entity -> [ModelEntity, ModelEntity]
So the final hirarchy is:
SomeEntity -> Entity -> [ModelEntity, ModelEntity].
Now, when applying the following lines, SomeEntity's collision component stays nil and I'm getting the following log message:
someEntity.generateCollisionShapes(recursive: true)
arView.scene.addAnchor(someEntity)
arView.installGestures([.all], for: someEntity)
The log message is:
[Collision] Bad paramater (SphereRadius), value = 0.000000, passed to shape creation.
It sounds like Entities with no model components breaks the recursive operation
Edit 1:
It works well for model entities created in code or in Reality Composer. for entities loaded from USDZ file the behavior is unexpected as the model hierarchy seems to play an important part.

Related

Entity framework reload entity from context

I have the following scenario:
I am using EF with Repository pattern and Unit Of Work from here
Then I extended my partial class and have created a Finder class which have GetAll and other methods.
You can this see below:
Below you can see that I am using a unit of work class with repositories of all classes to get the instance from the generic repository:
protected Repository<Category> _CategoryRepository;
public Repository<Category> CategoryRepository
{
get { return _CategoryRepository ?? (_CategoryRepository = new Repository<Category>(context)); }
}
So in this way I had different repositories and when getting an entity from db and updating it from a different context caused problems. So I used this method to use for context lifetime management. And it resolved that issue.
Now the problem I am facing is in the following code:
var cat = Instance.Category.GetSingle(c => c.CategoryID == 7);
var orignal = cat.CategoryName;
var expected = cat.CategoryName + " Test Catg Update";
cat.CategoryName = expected;
cat.Update(); //This doesn't actually update due to a validation in place (which is correct)
cat = Instance.Category.GetSingle(c => c.CategoryID == 7);
Assert.AreEqual(cat.CategoryName, expected);
When I use Update and I have some validators in it, and the Update fails (for example due to size of the string exceeds 15 characters). When I try to call GetSingle (2nd last line of above code) again, it brings me the same old record which is [cat.CategoryName + " Test Catg Update"]. Is this a normal way? If not how can this be fixed so I can reload the object from database.
Let me know if you need any other code or reference.
in your Update() method, if the validation fails, simply set the state of the entity to EntityState.Unchanged. Under the covers, changing the state of an entity from Modified to Unchanged first sets the values of all properties to the original values that were read from the database when it was queried, and then marks the entity as Unchanged. This will also reject changes to FK relationships since the original value of the FK will be restored.
You may be able to take advantage of the ObjectContext.Refresh() method to handle this as well. However, this is on the ObjectContext, so you would need a method to handle it in your UoW. The refresh method would be something like context.Refresh(RefreshMode.ServerWins, entity);

Entity Framework RIA Services How do I include entities within an included entity

I have the following hierarchy:
AccountCatagory
AccountType
AccoungGroup
GeneralLedger
SubsidiaryLedger
so each AccountCatagory has AccountTypes, each AccountType has AccountGroups...
Which needs to be loaded in to a tree view:
I need to load everything when the account category is loaded and I wrote it like this:
public IQueryable<AccountCatagory> GetAccountCatagories()
{
return this.ObjectContext.AccountCatagories.Include("AccountTypes");
}
Which works fine but only loads AccountTypes. within each AccountCatagory.
Writing an Include on each of the entities GetQuery doesn’t work.
How do I tell RIA services to Include entity when the Included entity also has an include/entity to load?
Here is an example of the way I got mine to work, assuming that all of your foreign keys are set up properly and the proper changes have been made in the metadata file of your Domain Service:
return ObjectContext.Users.Include("AccessRole")
.Include("AccessRole.AccessRoleReports")
I have never tried including objects with so many subtypes, but I assume this may work:
return this.ObjectContext.AccountCatagories.Include("AccountTypes")
.Include("AccountTypes.AccountGroups")
.Include("AccountTypes.AccountGroups.GeneralLedger")
.Include("AccountTypes.AccountGroups.GeneralLedger.SubsidiaryLedger");

Symfony form gets messy when calling getObject() in form configuration

I have a Strain model that has a belongsTo relationship with a Sample model, i. e. a strain belongs to a sample.
I am configuring a hidden field in the StrainForm configure() method this way:
$defaultId = (int)$this->getObject()->getSample()->getTable()->getDefaultSampleId();
$this->setWidget('sample_id', new sfWidgetFormInputHidden(array('default' => $defaultId)));
Whenever I create a new Strain, the $form->save() fails. The debug toolbar revealed that it tries to save a Sample object first and I do not know why.
However, if I retrieve the default sample ID using the table it works like a charm:
$defaultId = (int)Doctrine_Core::getTable('Sample')->getDefaultSampleId();
$this->setWidget('sample_id', new sfWidgetFormInputHidden(array('default' => $defaultId)));
My question here is what can be happening with the getObject()->getSample()... sequence of methods that causes the StrainForm to think it has to save a Sample object instead of Strain.
I tried to debug with xdebug but I cannot came up with a clear conclusion.
Any thoughts?
Thanks!!
When you call getSample its creating a Sample instance. This is automatically attached to the Strain object, thus when you save you also save the Sample.
An altenrative to calling getSample would be to chain through Strain object to the Sample table since i assume youre only doing this so your not hardcodeing the Sample's name in related form:
// note Sample is the alias not necessarily the Model name
$defaultId = Doctrine_Core::getTable($this->getObject()->getTable()->getRelation('Sample')->getModel())->getDefaultId();
Your solution probably falls over because you can't use getObject() on a new form (as at that stage the object simply doesn't exist).
Edit: Why don't you pass the default Sample in via the options array and then access it from within the form class via $this->getOption('Sample') (if I remember correctly)?

Problem with EF STE and Self-Referencing tables

This is my first post here, so I hope everything is fine.
Here is my problem:
I have a table in my database called UserTypes. It has:
ID;
IsPrivate;
Parent_ID;
The relevant ones are the first and the third one.
I have another table called UserTypes_T which has information for the different types, that is language specific. The fields are:
Language_ID;
UserType_ID;
Name;
What I'm trying to achieve is load the entire hierarchy from the UserTypes table and show it in a TreeView (this is not relevant for now). Then, by selecting some of the user types I can edit them in separate edit box (the name) and a combo box (the parent).
Everything works fine until I try to persist the changes in the database. EF has generated for me two entity classes for those tables:
The class for the user types has:
ID;
IsPrivate;
Parent_ID;
A navigational property for the self-reference (0..1);
A navigational property for the child elements;
Another navigational property for the UserTypes_T table (1..*);
The class for the translated information has:
UserType_ID;
Language_ID;
Name;
A navigational property to the UserTypes table (*..1);
A navigational property to the Languages table (*..1);
I get the data I need using:
return context.UserTypes.Include("UserTypes_T").Where(ut => ut.IsPrivate==false).ToList();
in my WCF Web service. I can add new user types with no problems, but when I try to update the old ones, some strange things happen.
If I update a root element (Parent_ID==null) everything works!
If I update an element where Parent_ID!=null I get the following error:
AcceptChanges cannot continue because the object’s key values conflict with another object in the ObjectStateManager.
I searched all over the internet and read the blog post from Diego B Vega (and many more) but my problem is different. When I change a parent user type, I actually change the Parent_ID property, not the navigational property. I always try to work with the IDs, not the generated navigational properties in order to avoid problems.
I did a little research, tried to see what is the object graph that I get and saw that there were lots of duplicate entities:
The root element had a list of its child elements. Each child element had a back reference to the root or to its parent and so on. You can imagine. As I wasn't using those navigational properties, because I used the IDs to get/set the data I needed, I deleted them from the model. To be specific I deleted points 4 and 5 from the UserTypes entity class. Then I had an object graph with each element only once. I tried a new update but I had the same problem:
The root element was updated fine, but the elements, that had some parents, threw the same exception.
I saw that I had a navigational property in the UserTypes_T entity class, pointing to a user type, so I deleted it too. Then this error disappeared. All the items in the object graph were unique. But the problem remained - I could update my root element with no problems, but when trying to update the children (with no exclusions) I got a null reference exception in the generated Model.Context.Extensions class:
if (!context.ObjectStateManager.TryGetObjectStateEntry(entityInSet.Item2, out entry))
{
context.AddObject(entityInSet.Item1, entityInSet.Item2);//here!
}
I tried to update only the name (which is in UserTypes_T) but the error is the same.
I'm out of ideas and I've been trying to solve this problem for 8 hours now, so I'll appreciate if someone gives me ideas or share their experience.
PS:
The only way I succeeded updating a child object was using the following code to retrieve the data:
var userTypes = argoContext.UserTypes.Include("UserTypes_T").Where(ut => ut.IsPrivate==false).ToList();
foreach (UserType ut in userTypes)
{
ut.UserType1 = null;
ut.UserTypes1 = null;
}
return userTypes;
where UserType1 is the navigational property, pointing to the parent user type and UserTypes1 is the navigational property, holding a list of the child element. The problem here was that EF "fixups" the objects and changes the Parent_ID to null. If I set it back again, EF sets the UserTypes1, too... Maybe there is a way to stop this behavior?
OK everybody, I just found what the problem was and I'm posting the answer if anybody else encounters the same issue.
The problem was that I was making some validation on the server in order to see if there isn't a circular reference between the user types. So, my method on the server looked something like:
using (MyEntities context = new MyEntities())
{
string errMsg = MyValidator.ValidateSomething(context.UserTypes,...);
if (!string.IsNullOrEmpty(errMsg)) throw new FaultException(errMsg);
//some other code here...
context.UserTypes.ApplyChanges(_userType);//_userType is the one that is updated
context.UserTypes.SaveChanges();
}
The problem is that when making the validation, the context is filled and when trying to save the changes, there are objects with the same key values.
The solution is simple - to use different context for validating things on the server:
using (MyEntities validationContext = new MyEntities())
{
//validation goes here...
}
using (MyEntities context = new MyEntities())
{
//saving changes and other processing...
}
Another one can be:
using (MyEntities context = new MyEntities())
{
using (MyEntities validationContext = new MyEntities())
{
//validation
}
//saving changes and other processing...
}
That's it! I hope it can be useful to somebody!

ASP.NET MVC2 Posting a ViewModel mapping to Domain (LINQ) to Submitting changes

I'm using AutoMapper to map between a Linq Domain object and a ViewModel to display an Edit Form to the user which works perfectly.
When they click submit I'd like to know the best way to map the ViewModel back to a Linq entity and persist it to the database.
The Linq entity I'm using has multiple collections of other entities (ie one-to-many references).
I was trying:
build up my custom view model using UpdateModel
get the previous state of the Linq entity by using the passed in id
map the view model onto the Linq entity (using automapper)
call SubmitChanges() on the data context
This method works when I'm only updating properties that are not collections, but throws errors when trying to modify properties that are collections.
Any help/thoughts would be much appreciated :)
I've taken an approach that is very similar to that used by Jimmy Bogard in the CodeCampServer project (http://codecampserver.codeplex.com/)
I have a general Mapper class that I inherit from where I just need to override a MapToModel method that maps from the ViewModel to the domain Model, and a GetIdFromViewModel method that returns the proper Id form the ViewModel so that the Service layer can grab the domain model from the database.
I had to change a little from the CodeCampServer examples because some of my Models used Guid and some used int as the Id for the Model.
You should be able to grab the code from the codeplex link above and that should help get you going in that direction.
Here is what one of my Mappers for a Member looks like:
public class MemberMapper : AutoFormMapper<Member, MemberFormViewModel, Guid>, IMemberMapper
{
public MemberMapper(IMemberService service) : base(service) { }
protected override Guid GetIdFromViewModel(MemberFormViewModel viewModel)
{
return viewModel.MemberId;
}
protected override void MapToModel(MemberFormViewModel viewModel, Member model)
{
// if the need arises, we will need to map the full objects as Foreign Key properties
// by using the proper repositories
// right now for loading the object to save back to the DB we don't have that need, so let's not waste the call
model.MemberId = viewModel.MemberId;
model.FirstName = viewModel.FirstName;
model.LastName = viewModel.LastName;
model.Title = viewModel.Title;
model.EmailAddress = viewModel.EmailAddress;
model.DirectPhone = viewModel.DirectPhone;
model.MobilePhone = viewModel.MobilePhone;
model.ElectronicId = viewModel.ElectronicId;
model.ProjectRoleTypeId = viewModel.ProjectRoleTypeId;
model.DepartmentId = viewModel.DepartmentId;
}
}
Then you can use this MemberMapper to map both directions. It uses AutoMapper to go from the domain Model to the View Model and then uses the MapToModel method that you implement to map from the View Model back to the domain Model.